The role of the "= =" operator
1, for the basic data type comparison
2, to determine whether the reference point to the same address heap memory.
equals Location:
In the object class, and object is the parent of all classes, included in the JDK, but not for most scenarios, it is often necessary to override
public boolean equals (Object obj) {return
(this = = obj);
}
The role of equals:
Used to determine whether two variables are references to the same object, that is, whether the contents of the heap are the same and the return value is a Boolean type
Basic use of equals:
Boolean B = Obj1.equals (OBJ2);
String types compare the content of different objects to the same, and you should use equals, because = = is used to compare reference types and compare basic data types with different capabilities.
The analysis is as follows:
String as an object to use
Example one: objects are different, content is the same, "= =" returns False,equals returns True
string S1 = new String ("Java");
String s2 = new String ("Java");
System.out.println (S1==S2); False
System.out.println (s1.equals (S2)); True
Example two: The same object, "= =" and equals result in the same
string S1 = new String ("Java");
String s2 = S1;
System.out.println (S1==S2); True
System.out.println (s1.equals (S2)); True
String as a basic type to use the
If the value is not the same, the object is not the same, so "= =" is the same as the equals result
String S1 = "Java";
String s2 = "java";
System.out.println (S1==S2); True
System.out.println (s1.equals (S2)); True
If there is no string object with the same value specified in the string buffer pool, the virtual machine creates a new string object for this purpose and is stored in a string buffer pool.
If there is a string object with the same value specified in the string buffer pool, then the virtual machine will not create a new string object for this purpose and return a reference to an existing string object directly.