title: Using Java's ternary operator, what's the implication of assigning a variable to itself?
body: <p>I make excessive use of the ternary operator, sometimes even replacing very simple if statements with it. For example, I just found myself replacing something like this:</p>
  
  <pre><code>if (foo != null) {
      bar = foo;
  }
  </code></pre>
  
  <p>with this:</p>
  
  <pre><code>bar = (foo == null) ? bar : foo;
  </code></pre>
  
  <p>While it is a question of taste which one reads nicer, I am wondering:  </p>
  
  <ul>
  <li><strong>In this case, is the ternary version's performance worse?</strong></li>
  <li><strong>Are there any other reasons against the use of ternary here?</strong></li>
  </ul>
  
  <p>The reason why I belive the performance could be worse here is that I assume the compiler can't optimize the ternary for when <code>foo</code> is <code>null</code>. Hence, the value of <code>bar</code> might be set to the same value again, wasting some CPU time, where the simple <code>if</code> statement would never get to that point. But my knowledge of compilers and execution is limited, hence the question.</p>