intern() behaving differently in Java 6 and Java 7

后端 未结 9 1047
梦谈多话
梦谈多话 2020-11-29 21:36
class Test {
    public static void main(String...args) {
        String s1 = \"Good\";
        s1 = s1 + \"morning\";
        System.out.println(s1.intern());
              


        
9条回答
  •  自闭症患者
    2020-11-29 22:18

    You need to use s1.equals(s2). Using == with String objects compares the object references themselves.

    Edit: When I run your second code snippet, I do not get "both are equal" printed out.

    Edit2: Clarified that references are compared when you use '=='.

提交回复
热议问题