Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent of register int?

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

register int i = 0;

I am aware that Java is an interpreted language, and is many many abstractions away from the CPU.

Is there any mechanism available to even request (and if the architecture doesn't allow it, so what) that my variable remains in a register instead of moving to cache or main memory?

I don't suppose there is any way, but I have been pleasantly surprised before.

Thank you,

like image 369
eleven81 Avatar asked Feb 11 '09 20:02

eleven81


2 Answers

register in C does not put a variable to register. It simply gives the compiler the hint, that it would probably be good to put it into a register.

In Java there is no equivalent.

like image 50
Johannes Weiss Avatar answered Sep 30 '22 04:09

Johannes Weiss


If it's used enough in a short space that making it a register int would be worthwhile, then the hotspot compiler should be able to figure that out itself.

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 have to guess; HotSpot can measure.

like image 21
Michael Myers Avatar answered Sep 30 '22 05:09

Michael Myers