Summary of the difference between Java== and equals

  • 2021-07-01 07:37:17
  • OfStack

The difference between = = and equals in Java feels that only a few people can say it completely correctly.

The common wrong answer is: = = the basic type compares whether the values are the same, and the reference type compares whether the references are the same; equals is whether the values are the same.

As to why it is wrong, you will know after reading the interpretation of = = and equals in this article.

1, = = Interpretation

The effect of = = is different for the base type and the reference type, as follows:

Basic type: Compare whether the values are the same; Reference type: Compare whether the references are the same;

Code example:


String x = "string";

String y = "string";

String z = new String("string");

System.out.println(x==y); // true

System.out.println(x==z); // false

System.out.println(x.equals(y)); // true

System.out.println(x.equals(z)); // true

Code interpretation: Because x and y point to the same reference, = = is also true, while new String () method rewrites and opens up memory space, so = = results in false, while equals comparison of 1 is a value, so the results are both true.

2. Interpretation of equals

equals is essentially = =, except that String, Integer and others override the equals method and turn it into a value comparison. Look at the following code to understand.

First, let's look at equals comparing 1 object with the same value by default. The code is as follows:


class Cat {

  public Cat(String name) {

    this.name = name;

  }

 

  private String name;

 

  public String getName() {

    return name;

  }

 

  public void setName(String name) {

    this.name = name;

  }

}

 

Cat c1 = new Cat(" Wang Lei ");

Cat c2 = new Cat(" Wang Lei ");

System.out.println(c1.equals(c2)); // false

The output result is beyond our expectation, and it turned out to be false? This is how to return a responsibility, see equals source code to know, source code is as follows:


public boolean equals(Object obj) {

    return (this == obj);

}

It turns out that equals is essentially = =.

Then the question arises, why do two String objects with the same value return true? The code is as follows:


String s1 = new String(" Lao Wang ");

String s2 = new String(" Lao Wang ");

System.out.println(s1.equals(s2)); // true

Similarly, when we go into the equals method of String, we find the answer, and the code is as follows:


public boolean equals(Object anObject) {

  if (this == anObject) {

    return true;

  }

  if (anObject instanceof String) {

    String anotherString = (String)anObject;

    int n = value.length;

    if (n == anotherString.value.length) {

      char v1[] = value;

      char v2[] = anotherString.value;

      int i = 0;

      while (n-- != 0) {

        if (v1[i] != v2[i])

          return false;

        i++;

      }

      return true;

    }

  }

  return false;

}

It turned out that String overrides Object's equals method, changing reference comparisons to value comparisons.

3. Summary

In general, = = is a comparison of values for primitive types and a comparison of references for reference types; By default, equals is reference comparison, but many classes override equals method, such as String, Integer, etc., which turns it into value comparison, so equals compares whether values are equal or not.


Related articles: