public static void main(String[] args) { String a = new String("myString"); String b = "myString"; String c = "my" + "String"; String d = c; System.out.print(a == b); System.out.print(a == c); System.out.print(b == c); System.out.print(b == d); }
public static void main(String[] args) { String a = new String("myString"); String b = "myString"; String c = "my" + "String"; String d = c; System.out.print(a == b); System.out.print(a == c); System.out.print(b == c); System.out.print(b == d); }
System.out.print(a == b)打印出来的是false
System.out.print(a == c)打印出来的是true
System.out.print(b == c)打印出来的是false
System.out.print(b == d)打印出来的是true
class StringJoin{ public static void main(String[] args){ String s1="a"; String s2="b"; String s6="ab"; String s3="a"+"b"; String s4=s1+s2; String s5=s1+"b"; //常量拼接与常量比较 System.out.println(s6==s3); //常量与变量拼接与常量比较 System.out.println(s6==s5); //变量拼接与常量比较 System.out.println(s6==s4); } }运行结果: