public class Test {
public static void Main (string[] args) {
String a = null;
String B = null;
String C = "";
String d = "";
String e = new String ("abc");
String f = "abc";
System.out.println ("a = = B:" + (A = = b));
System.out.println ("c = = d:" + (c = = d));
System.out.println ("E = = f:" + (E = = f));
}
}
Console output:
A = = B:true
c = = D:true
E = = F:false
The first A and B comparisons are null, so a is equal to B.
The second "" string is an anonymous object, and both C and D point to the anonymous object, and the heap memory address is the same, so C is equal to D.
The third e actually creates 2 string objects, one is an anonymous object of "ABC", one is a heap memory space object through the new keyword, and then e points to the string object created by new, and F is actually the anonymous object of "ABC". So f and e are not equal.
String g = new string (null); This method is not possible, the compile time will be error.
Application analysis of String class in Java, double equal sign comparison string object