As Rogério answered, getting the object reference outside the loop (Object object = example.getValue();
) will likely be faster (or will at least never be slower) than calling the getter inside the loop because
- in the "worst" case,
example.getValue()
might actually do some very computationally-expensive stuff in the background despite that getter methods are supposed to be "trivial". By assigning a reference once and re-using it, you do this expensive computation only once.
- in the "best" case,
example.getValue()
does something trivial such as return value;
and so assigning it inside the loop would be no more expensive than outside the loop after the JIT compiler inlines the code.
However, more important is the difference in semantics between the two and its possible effects in a multi-threaded environment: If the state of the object example
changes in a way which causes example.getValue()
to return references to different objects, it is possible that, in each iteration, the method doSomething(Object object)
will actually operate on a different instance of Object
by directly calling doSomething(example.getValue());
. On the other hand, by calling a getter outside the loop and setting a reference to the returned instance (Object object = example.getValue();
), doSomething(object);
will operate on object
n times for n iterations.
This difference in semantics can cause behavior in a multi-threaded environment to be radically different from that in a single-threaded environment. Moreover, this need not be an actual "in-memory" multi-threading issue: If example.getValue()
depends on e.g. database/HDD/network resources, it is possible that this data changes during execution of the loop, making it possible that a different object is returned even if the Java application itself is single-threaded. For this reason, it is best to consider what you actually want to accomplish with your loop and to then choose the option which best reflects the intended behavior.