String Pool behavior

前端 未结 6 1622
迷失自我
迷失自我 2021-01-18 02:55

I read this Questions about Java's String pool and understand the basic concept of string pool but still don\'t understand the behavior.

First: it works if you

6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-18 03:42

    The compiler can perform constant evaluation but not in the case where you modify the values

    Try instead following and see what happens if you drop final from either variable.

    final String s1 = "abc";
    final String s2 = "abc";
    System.out.println("s1 == s2? " + (s1 == s2));
    
    String s3 = s1 + "d";                  
    String s4 = s2 + "d";
    System.out.println("s3 == s4? " + (s3 == s4));
    

提交回复
热议问题