Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strike>It compiles to the exact same byte code.</strike> It's all a matter of preference.</p> <p><strong>EDIT</strong>:<br>As it turns out this is <strong>NOT</strong> true.<br></p> <pre><code>public class SO_Test { public static void main(String[] args) { int a = 1; a++; a += 1; ++a; } } </code></pre> <p>Output: <img src="https://i.stack.imgur.com/z8oCN.png" alt="enter image description here"></p> <p>Example:</p> <pre><code>public class SO_Test { public static void main(String[] args) { int a = 1; a = a + 1; a++; a += 1; ++a; } } </code></pre> <p>Output: <img src="https://i.stack.imgur.com/ifNTG.png" alt="enter image description here"></p> <p>The differences can be analyzed on the <a href="http://en.wikipedia.org/wiki/Java_bytecode_instruction_listings" rel="noreferrer">Java bytecode instruction listings page</a>. In short, <code>a = a + 1</code> issues <code>iload_1</code>, <code>iconst_1</code>, <code>iadd</code> and <code>istore_1</code>, whereas the others only use <code>iinc</code>.</p> <p>From @NPE:</p> <blockquote> <p>The prevailing philosophy is that javac deliberately chooses not to optimize generated code, relying on the JIT compiler to do that at runtime. The latter has far better information about the execution environment (hardware architecture etc) as well as how the code is being used at runtime.</p> </blockquote> <p>So in conclusion, besides not compiling to the same byte code, with exceedingly high probability, it won't make a difference. It's just a stylistic choice.</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