Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Have you <strong>profiled</strong> your <strong>opinion</strong> lately. </p> <p><strong>Performance:</strong></p> <p>This might have been a micro-optimization that was something to be concerned with in 1999-2001 in a pre-1.2 JVM and even then I would have questioned it unless some serious numbers showed otherwise.</p> <p>Modern JIT implementations would tell you that today that your <strong>opinion</strong> is out of place.</p> <p>Modern compiler implementations do all kinds of optimizations that make thinking about things like this a waste of time in Java. The JIT just makes it even more of a waste of concern.</p> <p><strong>Logic:</strong></p> <p>In a concurrent situation, your two code blocks are not logically equivalent, if you want to see changes, making the local copy will prevent that. Depending on what you want to do, one or the other approaches could create very subtle non-deterministic bugs that would be very hard to pin down in more complicated code. </p> <p>Especially if what was return was something mutable unlike a <code>String</code> which is immutable. Then even the local copy could be changing, unless you did a deep clone, and that gets really easy to get wrong really quickly.</p> <p><strong>Concern yourself</strong> with doing it <strong>correctly</strong>, then <strong>measure and then optimize what is important</strong> as long as it doesn't make the code less maintainable.</p> <p>The JVM will inline any calls to <code>final</code> instance members and remove the method call if there is nothing in the method call other than the <code>return this.name;</code> It knows that there is not logic in the accessor method and it knows the reference is <code>final</code> so it knows it can inline the value because it will not be changing.</p> <p>To that end</p> <pre><code>person.getName() != null &amp;&amp; person.getName().equalsIgnoreCase("Einstein") </code></pre> <p>is expressed more correctly as</p> <pre><code>person != null &amp;&amp; "Einstein".equalsIgnoreCase(person.getName()) </code></pre> <p>because there is no chance of having a <code>NullPointerException</code></p> <p><strong>Refactoring:</strong></p> <p>Modern IDE refactoring tools remove any arguments about having to change code in a bunch of places as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload