Strange behavior with string interning in Java

后端 未结 4 1646
别跟我提以往
别跟我提以往 2020-12-14 10:21

There is code as following:

String s = new String(\"1\");
s.intern();
String s2 = \"1\";
System.out.println(s == s2);

String s3 = new String(\"1\")+new          


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 10:53

    s.intern() doesn't change the string s. You should have written:

        s = s.intern();
    

提交回复
热议问题