Reputation: 135
I was wondering when you do a calculation in java like this:
float x;
if (x/2f>10f) {}
does it have to do the division every time you run it or during the compilation stage does it inline it with the correct value?
I'm guessing the latter since the value of x can change? Am I making sense? I want to know if it's worth my time to precalculate certain values instead of repeating them or if its already done for me.
As you can tell, I don't have any formal education with programming, I'm just curious about it.
If I'm using the word inlining incorrectly please excuse me, as I haven't attempted C in a long time.
edit: thanks for replying, it seems obvious to me, and i feel silly for asking, that it cant be compile time if the value of x is variable, but what if its initialized and declared as final? because i read that operations on literals are compile time, so how about constants...so...
final float x=1f; if (x/2f>10f) {}
Upvotes: 3
Views: 147
Reputation: 41
Since "x" is not initialized. calculation will be done at run-time.
Upvotes: 0
Reputation: 339
In the code snipped that you provide, assuming that it's inside a method, "x" is not initialized, this step is checked at compile stage, calculating is done at runtime.
Upvotes: 1