1. When both sides of an expression are literals, calculations will be performed during the compilation process and the results will be generated at the position of the expression to save computing efficiency;
ps: jvm thinks that only when it is a literal, it will compare the contents of the constant pool; if it is new, the new object is a different object.
example 1:
<span style="color: #008080">1</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span><span style="color: #000000"> main(String[] args){ </span><span style="color: #008080">2</span> String str = "123str"<span style="color: #000000">; </span><span style="color: #008080">3</span> String str1 = "123"+"str"<span style="color: #000000">; </span><span style="color: #008080">4</span> String str2 = <span style="color: #0000ff">new</span> String("123str"<span style="color: #000000">); </span><span style="color: #008080">5</span> <span style="color: #008080">6</span> System.out.println(str == str1); <span style="color: #008000">//</span><span style="color: #008000">true</span> <span style="color: #008080">7</span> System.out.println(str == str2); <span style="color: #008000">//</span><span style="color: #008000">false</span> <span style="color: #008080">8</span> }
The above is the detailed content of java compiler features. For more information, please follow other related articles on the PHP Chinese website!