In Java, the difference between equals and equals is a required interview question, yet very few candidates can answer it correctly.

Common wrong answers are: == Base types compare whether values are the same, reference types compare whether references are the same; Equals is whether the values are the same.

As to why this is wrong, read this article about equals and equals.

1, == interpretation

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

  • Basic type: compares values to be the same;
  • Reference type: compares whether references are the same;

Example code:

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 the code

The new String() method overwrites the memory space, so == is false. The equals method always compares values, so == is true.

2. Equals interpretation

Equals is essentially equals, except that strings and integers override equals and turn it into a value comparison. See the code below.

Equals () compares an object with the same value by default.

class Cat {
    public Cat(String name) {
        this.name = name;
    }

    private String name;

    public String getName(a) {
        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
Copy the code

The output is, to our surprise, false? The equals source code is as follows:

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

So equals is essentially equals equals.

So why do two strings with the same value return true? The code is as follows:

String s1 = new String("Wang");
String s2 = new String("Wang");
System.out.println(s1.equals(s2)); // true
Copy the code

Similarly, when we enter the equals method of String and find the answer, the code looks like this:

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 the code

String overrides equals () to compare values instead of references.

3, summarize

In general, == is a value comparison for basic types, and a comparison is a reference for reference types; Equals by default is a reference comparison, but many classes override equals to make it a value comparison, such as String, Integer, and so on. In general, equals compares values.

Scan the qr code below for more updates:

Recommended articles:

Java’s most common 200+ interview questions

Top resumes for programmers – a must for interview