The difference between == and equal in Java

  • 2020-04-01 02:22:35
  • OfStack

Without further ado, let's start with a piece of code:


String str1 = new String("str");
        String str2 = new String("str");
        System.out.println("== To compare   : "+ str1 == str2);
        System.out.println("equal Comparison: "+ str1.equals(str2));
        String str3 = "str1";
        String str4 = "str1";
        System.out.println("== To compare   : "+ str3 == str4);
        System.out.println("equal Comparison: "+ str3.equals(str4));

Output answers:

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201310/20131025165411329.png" >

Based on the print, we can see that if we use equal to instantiate with autoboxing or new to instantiate, we return true, whereas if we use ==, we return true with autoboxing to instantiate, and we use new to instantiate

Instantiated returns true false; Instead of trying to figure out why, let's learn the difference between equals and == so we can get the answer

The equals method is initially defined in the base class Object of all classes, and the source code is


 public boolean equals(Object obj) {
    return (this == obj);
    }

You can see that the equals defined here is equivalent to ==, but how can the equals defined above be different?
The reason is that the String class overwrites equals:


public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
        char v1[] = value;
        char v2[] = anotherString.value;
        int i = offset;
        int j = anotherString.offset;
        while (n-- != 0) {
            if (v1[i++] != v2[j++])
            return false;
        }
        return true;
        }
    }
    return false;
    }

Here are five points to refocus on equals:

1     Reflexivity: for any reference value X, the return value of x.exquals (X) must be true.
2     Symmetry: for any reference value x,y, the return value of x.quals (y) must be true if and only if the return value of y.quals (x) is true;
3     Transitivity: if x.equals(y)=true and y.equals(z)=true, then x.equals(z)=true
4     Consistency: if there is no change in the object being compared, there should be no change in the result of the object being compared
5     Nonnullity: any nonnull reference value X, x.quals (null) must return false

After rewriting, it is essentially different from == :

Equal: is used to compare the contents inside two objects to be equal. Since all classes inherit from the java.lang.object class, it is called if the method is not overwritten
Is still the method in the Object class, while the equal method in Object returns the judgment of ==, so if the method is not overwritten, the method is not called
Any meaning. In Java object-oriented processing, we usually choose to override the equals method in javabeans. After using hibernate, we will generate the mapping files and entities of the database

Class. This is the best way to override the equals method in the entity class. When overriding, we can implement the method according to our own definition as long as we abide by the five principles, for example, for a student class

We define that we consider these two objects to be equal as long as the student Numbers are the same; At the same time, we also override the hashcode method (link: #) == : to determine whether two objects have the same address, that is, whether they refer to the same object. This compares to a pointer operation in the true sense.


Related articles: