public class StringDemo {
public void test() {
String a = "a";
String b="a";
String c=a;
String aa = new String("a");
String bb = new String("a");
String cc = aa;
String ab = "ab";
String ab2 = "a"+"b";
String ab3 = a + b;
String ab4 = aa + bb;
System.out.println();
System.out.println(String.format("a==b:%s",a==b));
System.out.println(String.format("a==c:%s",a==c));
System.out.println(String.format("a==aa:%s",a==aa));
System.out.println(String.format("a==bb:%s",a==bb));
System.out.println(String.format("a==cc:%s",a==cc));
System.out.println(String.format("aa==bb:%s",aa==bb));
System.out.println(String.format("aa==cc:%s",aa==cc));
System.out.println(String.format("ab==ab2:%s",ab==ab2));
System.out.println(String.format("ab==ab3:%s",ab==ab3));
System.out.println(String.format("ab==ab4:%s",ab==ab4));
}
}

结果:

a==b:true
a==c:true
a==aa:false
a==bb:false
a==cc:false
aa==bb:false
aa==cc:true
ab==ab2:true
ab==ab3:false
ab==ab4:false

内存标识:

java字符串类型String测试_oo