Detailed explanation of the difference between == and equals in Java

不言
Release: 2019-03-07 17:34:48
forward
3626 people have browsed it

This article brings you a detailed explanation of the difference between == and equals in Java. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

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

The common wrong answer is: == Basic types compare whether the values ​​are the same, reference types compare whether the references are the same; and equals compares whether the values ​​are the same.

As for why it is wrong, you will know after reading this article’s interpretation of == and equals.

1. == Interpretation

The effects of == are different for basic types and reference types, as follows:

Basic types : The comparison is whether the values ​​are the same; reference type: the comparison is 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
Copy after login

Code interpretation: Because x and y point to the same reference, so = = is also true, and the new String() method is rewritten to open up memory space, so the result of == is false, and equals always compares values, so the results are all true.

2. Interpretation of equals

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

First let’s look at how equals compares objects 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("王磊");
Cat c2 = new Cat("王磊");
System.out.println(c1.equals(c2)); // false
Copy after login

The output result is beyond our expectation, it is false? What is going on? You will know after reading the source code of equals. The source code is as follows:

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

It turns out that equals is essentially ==.

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

String s1 = new String("老王");
String s2 = new String("老王");
System.out.println(s1.equals(s2)); // true
Copy after login

Similarly, when we enter the equals method of String, we find the answer. 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;
}
Copy after login

It turns out that String overrides the equals method of Object and changes the reference comparison. It becomes a value comparison.

3. Summary

Generally speaking, == is a value comparison for basic types, and for reference types, it is a reference; and equals defaults In this case, it is a reference comparison, but many classes have overridden the equals method, such as String, Integer, etc., turning it into a value comparison, so under normal circumstances, equals compares whether the values ​​are equal.

The above is the detailed content of Detailed explanation of the difference between == and equals in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template