String a = "ab"; String b = new String("ab"); System.out.println(a == b); System.out.println(a.equals(b)); System.out.println(b.intern() == a); System.out.println(a.intern() == b);
打印结果:
false
true
true
false
new String不会去String常量迟中找已有的字符串。string.intern()方法是在字符常量池里面找相等的字符串。