Recently, the question arose of whether it was a good idea in Java to assign the results of a getter call to a local variable in order to avoid multiple calls of the same access. I can’t find the original post, but it seems that consensus is usually not necessary, since Hotspot optimizes the delay in calling the method anyway.
However, what is the point of view of using this technique to avoid multiple throws? At the moment, I am faced with a choice between:
if (a instanceof Foo) { // Cast once and assign to local variable. Foo foo = (Foo)a; if (foo.getB() == 1 && foo.getC() == 2) { ... } }
OR
if (a instanceof Foo) {
java casting
Adamski
source share