This isn’t meant to be subjective, I am looking for reasons based on resource utilisation, compiler performance, GC performance etc. rather than elegance. Oh, and the position of brackets doesn’t count, so no stylistic comments please.
Take the following loop;
Integer total = new Integer(0);
Integer i;
for (String str : string_list)
{
i = Integer.parse(str);
total += i;
}
versus…
Integer total = 0;
for (String str : string_list)
{
Integer i = Integer.parse(str);
total += i;
}
In the first one i is function scoped whereas in the second it is scoped in the loop. I have always thought (believed) that the first one would be more efficient because it just references an existing variable already allocated on the stack, whereas the second one would be pushing and popping i each iteration of the loop.
There are quite a lot of other cases where I tend to scope variables more broadly than perhaps necessary so I thought I would ask here to clear up a gap in my knowledge. Also notice that assignment of the variable on initialisation either involving the new operator or not. Do any of these sorts of semi-stylistic semi-optimisations make any difference at all?
The second one is what I would prefer. There is no functional difference other than the scoping.
Setting the same variable in each iteration makes no difference because
Integeris an immutable class. Now, if you were modifying an object instead of creating a new one each time, then there would be a difference.And as a side note, in this code you should be using
intandInteger.parseInt()rather thanIntegerandInteger.parse(). You’re introducing quite a bit of unnecessary boxing and unboxing.Edit: It’s been a while since I mucked around in bytecode, so I thought I’d get my hands dirty again.
Here’s the test class I compiled:
Bytecode output (retrieved with
javap -c ScopeTestafter compiling):Contrary to my expectations, there was one difference between the two: in
outside(), the variableistill took up a register even though it was omitted from the actual code (note that all theiloadandistoreinstructions point one register higher).The JIT compiler should make short work of this difference, but still you can see that limiting scope is a good practice.
(And with regards to my earlier side note, you can see that to add two Integer objects, Java must unbox both with
intValue, add them, and then create a new Integer withvalueOf. Don’t do this unless absolutely necessary, because it’s senseless and slower.)