Java Method invocation vs using a variable

Never code for performance, always code for readability. Let the compiler do the work.

They can improve the compiler/runtime to run good code faster and suddenly your “Fast” code is actually slowing the system down.

Java compiler & runtime optimizations seem to address more common/readable code first, so your “Optimized” code is more likely to be de-optimized at a later time than code that was just written cleanly.

Note:

This answer is referring to Java code “Tricks” like the question referenced, not bad programming that might raise the level of loops from an O(N) to an O(N^2). Generally write clean, DRY code and wait for an operation to take noticeably too long before fixing it. You will almost never reach this point unless you are a game designer.

Leave a Comment