java - comparison of objects


Comparing objects of the same type

Sample introduction

equality judgment


Comparing objects of the same type

three dimensions to compare




Sample introduction

Imagine a scene like this: Xiao Wang went to the library to borrow a Java Core Technology Volume 1, as shown in the figure

Unfortunately, Xiao Wang lost the book, he bought two more Java Core Technology Volume 1


new book 1 new book 2

If Xiao Wang returns the book with the newly bought book 1

1. If the library prohibits him from returning the book // that is not the same (not the same book)

2. If the library allows him to return books // have "equality" (not the same book, as long as they are equal)

Ruo Xiaowang uses the newly bought book 2 to return the book

3. If the library allows him to return the book //with similarity

Book b1 = new Book("white-skinned java core volume 1"); <-> identity relationship between b1 and b1

Book b2 = new Book("white-skinned java core volume 1"); o1==o2 <-- is to judge the relationship, the judgment is the identity,

Book b3 = new Book("Huangpi's java core volume 1"); That is, whether o1 and o2 point to the same object.


b1 == b1 => true

b1 == b2 => false

b1 == b3 => false

equality judgment

There is no native logic to provide similarity judgment in java, but there are constraints on equality judgment, namely equals (equal)

There is an equals method from the Object class. + All classes inherit from Object. => All classes have their own equals method

Java expects the implementer of the class (who defines the class) to write the correct equals method to ensure equality

Hope: b1.equals(b1) => true b1.equals(b3) => false

           b1.equals(b2) =>true

  1. public class Main {
  2. public static void main ( String[] args ) {
  3. Book b1 = new Book( "white" , "java core volume 1" );
  4. Book b2 = new Book( "white" , "java core volume 1" );
  5. Book b3 = new Book( "yellow" , "java core volume 1" );
  6. // check for identity
  7. System.out.println ( b1 ==b1); //true
  8. System.out.println ( b2 ==b2); //true
  9. System.out.println ( b3 ==b3); //true
  10. System.out.println ( b1 ==b2); //false
  11. System.out.println ( b1 ==b3); //false
  12. //check equality
  13. System.out.println (b1.equals( b1 )); // true
  14. System.out.println (b1.equals( b2 ) ); //hope to be true//actually print false
  15. System.out.println (b1.equals( b3 ) ); //false
  16. }
  17. }

In the above example, b1.equals(b2) is false. The reason is that Book does not override equals, so the equals method of its parent class Object is executed. In Object, equals is still judging the identity, so the result naturally displays false . 

If the method wants b1.equals(b2) to be true, it needs to rewrite the equals method of Book correctly.

Correctness must be guaranteed after overriding the method

What is correctness:

1. Reflexivity => b1.equals(b1) is true

2, when b1.equals(b2)=>true ;b2.equals(b2) =>true

3. Transitivity

        b1.equals(b2) => true && b2.equals(b3) =>true to get b1.equals(b3) =>true

4, and the result of null is generally false

        b1.equals(null) => false

You don't need to write it yourself here, just use the tool to generate the correct equals

Code -> Generate -> equals();

  1. public class Book {
  2. @Override
  3. public boolean equals ( Object o ) {
  4. if ( this == o ) return true ;
  5. if (o == null || getClass () != o. getClass ()) return false ;
  6. Book book = ( Book ) o;
  7. return color.equals ( book.color ) &&
  8. name. equals (book. name );
  9. }
  10. public String color;
  11. public String name;
  12. public Book ( String color , String name) {
  13. this.color = color ;
  14. this . name = name;
  15. }

After rewriting the equals method, run it again. At this time, the equals method has been rewritten, and b1.equals(b2) is true.


Identity, equality, similarity (similarity java does not support)

identity and equality

== comparison: identity comparison (for primitive types, identity is equality; for reference types, identity is just identity)

To handle equality correctly

1, b1.equals(b2) 2, Book correctly rewrites the equals method

                                Satisfy several properties: (several properties of correctness listed above)

                                Generated with idea tool

Can this knowledge help you understand identity, equality, and similarity when learning programming? If you find any mistakes, please feel free to criticize and correct! !

If it is helpful to you, please support three consecutive times if you can, thank you! !


Related: java - comparison of objects