Java equivalent of register int?

In C, I can allocate case for a variable, for example:

register int i = 0; 

I know that Java is an interpreted language and has many abstractions from the processor.

Is there any mechanism available even for a request (and if the architecture does not allow it, and so on) that my variable remains in the register instead of going to the cache or main memory?

I don’t think there is any way, but I was pleasantly surprised before.

Thanks,

+7
java c cpu-registers
source share
6 answers

No, there is no way to request this in Java. However, there are some things you can do to prevent the use of the register, for example, applying the volatile to a member variable.

+3
source share

register in C does not put a variable to register. It just gives the compiler a hint that it would probably be nice to put it in a register.

There is no equivalent in Java.

+18
source share

If it used enough in short space, which makes it an int register, it would be useful, then the hotspot compiler would have to figure it out on its own.

In fact, the hotspot compiler should be able to do a better job than the C / C ++ compiler because it has more information to work with. C / C ++ compilers should guess; HotSpot can measure.

+6
source share

There is no equivalent in Java. Even in C there is no guarantee that a variable will be stored in a register, and compilers can ignore it.

In Java, the method will be interpreted until the JIT access point heuristically determines that it needs to be compiled. For compiled code, it uses a coloring algorithm to assign variables and temporary values ​​to registers or write to / from RAM in case of register overflow.

+5
source share

I know that Java is an interpreted language, and many abstractions from the processor.

You answered your question very well :-)

But seriously, in general, write your code as clearly and simply as you can, and the JVM will do its best to properly process your code.

+3
source share

You can create an annotation called @register, but the JVM will definitely ignore it. eg.

 @register int i = 0; 
0
source share

All Articles