闲话不说直接上例子:
(“ == ” 和 equals 返回的均为布尔类型)
例一:
String s1 = new String("abc");
String s2 = "abc";
System.out.println(s1 == s2); // 打印false
System.out.println(s1.equals(s2)); // 打印true
例二:
String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2); //打印true
System.out.println(s1.equals(s2)); //打印true
例三:
String s1 = "a" + "b" + "c";
String s2 = "abc";
System.out.println(s1 == s2); //打印true
System.out.println(s1.equals(s2)); //打印true
例四:
String s1 = "ab";
String s2 = "abc";
String s3 = s1 + "c";
System.out.println(s3 == s2); //打印false
System.out.println(s3.equals(s2)); //打印true