title: How does modern Java optimize a self assignment?
body: <p>I am using the following line of code over and over in a loop:</p>
  
  <pre><code>Object val;
  while (logic())
     val = val == null ? generateObj() : val;
  </code></pre>
  
  <p>I am curious how the JVM optimizes this code, if at all? I.e., if <code>val != null</code> will the JVM sense that the assignment goes back to self and just ignore the rest of the line, or will it take the time to copy the reference of <code>val</code> back over itself?</p>
  
  <p>I am running the standard JRE 6 and JRE 7 in my environments for Windows 7.</p>
  
  <p>ASIDE:
  If the JVM does not optimize this operation very well, what is the best alternative to doing this call? Just <code>if (val == null) val = generateObj();</code>?</p>