1、当一个表达式两边都是字面量,则编译过程中就会进行计算,并将结果生成在表达式的位置,以节省运算效率;
ps:jvm认为只有是字面量时,才会对比常量池中内容;而如果是new的新对象为不同对象。
例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> }
Atas ialah kandungan terperinci java编译器特性. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!