As we all know, data types in Java can be broadly divided into two categories: basic data types and reference data types. equals and = = are used to compare whether the data is "equal" to the data.
Comparison of basic data types
Comparisons between basic data types use "= =" to determine whether their values are equal.
Example code:
public class Test{public static void Main (string[] args) {int i1=9;int i2=9;boolean b1=true;boolean b2=true;char c1= ' C '; ch Ar c2= ' C '; System.out.println (I1==I2); Output is trueSystem.out.println (B1==B2); Output is trueSystem.out.println (C1==C2); Output is true}}
comparisons between the base data types do not use equals.
Comparison of reference data types
Reference types can be compared by "= =" and by equals.
Use "= =" To compare reference objects
When you compare reference objects with "= =", they are compared to their address, which is the value of the variable stored in the stack.
public class test{public static void Main (string[] args) {person p1=new person ("Danny"); Person P2=p1; Person P3=new person ("Danny",); System.out.println (P1==P2); The output is true System.out.println (P1==P3); The output is false }}class person{public String name; public int age; Public person (String Name,int age) { this.name=name; this.age=age;} }
the memory graph at this point is:
P1 and P2 all point to the same object, so the addresses stored in P1 and P2 are the same, and the "= =" Comparison is exactly what they store in the stack, so the value of P1==P2 is true; P3 and P1 point to the same object, but not the same object, so p1!= P3 (or P1==p3==false).
Special circumstances,
for string and integer, the way the object is created differs from the result of the "= =" comparison, such as:
public class Test{public static void Main (string[] args) {String a1= "Hello"; String a2= "Hello"; String A3=new string ("World"); String A4=new string ("World"); System.out.println (A1==A2); Output is trueSystem.out.println (A3==A4); Output to False}}
To compare reference types with equals
The Equals method is defined in the base class object, and this method compares the memory address of the object, that is, if the "P1==P2" is replaced with P1.equals (P2) in the previous example, the result is the same.
Special circumstances,
If a class does not have its own definition of the Equals method, its default Equals method (inherited from the object class) uses the = = operator, and if the object that the two variables point to is the same object, then using equals and using = will get the same result.
However, some classes override the method in the object class, when it compares the contents of the object being referred to. For example:
public class Test{public static void Main (string[] args) {String a1= "Hello"; String a2= "Hello"; String A3=new string ("Hello"); String A4=new string ("Hello"); System.out.println (A1==A2); Output is trueSystem.out.println (A3==A4); Output is FalseSystem.out.println (A1.equals (A4)); Output is True}}class person{public string name;public int age;public person (string Name,int age) {this.name=name;this.age= Age;}}
the above A1 and A4 clearly do not point to the same object, but they refer to the same content in the object, so the value of A1.equals (A4) is true.
J2SE the difference between equals and = = in fast advanced--java