Java for loop performance

The second is better - assuming someMethod() does not have side effects.
It actually caches the value calculated by someMethod() - so you won't have to recalculate it (assuming it is a relatively expansive op).

If it does (has side effects) - the two code snaps are not equivalent - and you should do what is correct.

Regarding the "size for variable a" - it is not an issue anyway, the returned value of someMethod() needs to be stored on some intermediate temp variable anyway before calculation (and even if it wasn't the case, the size of one integer is negligible).

P.S.
In some cases, compiler / JIT optimizer might optimize the first code into the second, assuming of course no side effects.


If in doubt, test. Use a profiler. Measure.


Assuming the iteration order isn't relevant, and also assuming you really want to nano-optimize your code, you may do this :

for (int i=someMethod(); i-->0;) {
  //some code
}

But an additional local variable (your a) isn't such a burden. In practice, this isn't much different from your second version.