This article is participating in the “Java Theme Month – Java Brush questions punch card”, see the activity link for details

Equals () = equals(

This article will take you through the equals() method

Second, code demonstration

Object.java

Indicates whether other objects are "equal" to this object. The equals method implements equivalence on non-empty object references: it is reflexive: for any non-empty reference value x, x.equals (x) should return true. It is symmetric: for any non-empty reference values x and y, x.quals (y) should return true if and only if y.quals (x) returns true. It is transitive: for any non-null reference values x, y, and z, if x.quals (y) returns true and y.quals (z) returns true, then x.quals (z) should return true. It is consistent: multiple calls to x.equals (y) always return true or false for any non-empty reference values x and y, provided that the information used in the equals comparison of objects has not been modified. For any non-null reference value x, x. beers (null) should return false. The equals method of a class object implements the most distinct equivalence relation on the object; That is, for any non-empty reference values x and y, this method returns true if and only if x and y refer to the same object (x==y is true). Note that whenever a hashCode method is overridden, it is usually necessary to override it in order to maintain the general convention of the hashCode method that equal objects must have equal hash codes. public boolean equals(Object obj) { return (this == obj); }Copy the code

Integer.java

Compares this object with the specified object. If and only if the argument is not null and is an integer object that contains the same int value as this object, Public Boolean equals(Object obj) {if (obj instanceof Integer) {return value == ((Integer)obj).intValue(); } return false; }Copy the code

Third, thinking and analysis

int a1=1; Integer a2=new Integer("1"); Integer a3=new Integer("1"); Integer a4=1; Integer a5=1; Integer a6=999; Integer a7=999; AssertTrue (a1== A2); // Assert. AssertTrue (a1== A2); AssertTrue (a1== A3); // Assert. AssertTrue (a1== A3); AssertTrue (a2==a1); // Assert. AssertTrue (a2==a1); AssertFalse (a2==a3); assertFalse(a2==a3); New assertTrue(a4==a5); new assertTrue(a4==a5); New assertFalse(a6==a7); Student o1=new Student(null); Student o2=new Student(null); Student o3=new Student("1111"); Student o4=new Student("1111"); Student o5=new Student("2222"); ArrayList<Student> list=new ArrayList(); HashSet<Student> set=new HashSet(); list.add(o1); set.add(o1); Assert.assertTrue(list.contains(o1)); Assert.assertTrue(list.contains(o2)); Assert.assertTrue(set.contains(o1)); AssertTrue (set.contains(O2));Copy the code

Four,

define

It is reflexive: for any non-empty reference value x, x.equals (x) should return true.

It is symmetric: for any non-empty reference values x and y, x.quals (y) should return true if and only if y.quals (x) returns true.

It is transitive: for any non-null reference values x, y, and z, if x.quals (y) returns true and y.quals (z) returns true, then x.quals (z) should return true.

It is consistent: multiple calls to x.equals (y) always return true or false for any non-empty reference values x and y, provided that the information used in the equals comparison of objects has not been modified.

use

For primitive types, the == operation compares values to be equal and for object types,

Compares the memory address of two objects

For comparison between a wrapper type and a primitive type, the compiler converts it to a primitive type and then compares it