What is the difference between == and equals in Java?
Please tell me why in the code below ==
and equals
give the same result?
import static java.lang.System.*;
public class A {
public static void main(String[] args){
B b1 = new B("one","two");
B b2 = new B("one", "two");
B b3 = b1;
// ==
out.println(b1 == b2); // false
out.println(b1 == b3); // true
out.println(b2 == b3); // false
// equals
out.println(b1.equals(b2)); // false
out.println(b1.equals(b3)); // true
out.println(b3.equals(b2)); // false
}
}
class B {
public B(String prop1, String prop2){
this.prop1 = prop1;
this.prop2 = prop2;
}
private String prop1 = null;
private String prop2 = null;
}
3 answers
Since the equals()
method is not overridden, this method compares references, as well as the ==
operator. You can see this if you look at the source code of the equals
method of the Object
class:
public boolean equals(Object obj) {
return (this == obj);
}
And if equals()
is overridden, take, for example, the class Integer
:
public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}
That:
Integer a = new Integer(6);
Integer b = new Integer(6);
System.out.println(a == b);
// false т.к. это разные объекты с разными ссылками
System.out.println(a.equals(b));
// true, т.к equals() переопределен и сравниваются уже не ссылки
To compare classes, you need to redefine the equals()
method (and then you should redefine hashCode()
as well).
The equals() method denotes the equivalence relation of objects. An equivalent relation is one that is symmetric, transitive, and reflexive. But we can override the equals method by setting the logic for comparing two objects. There is no difference between equals() and the == operation in the Object class. If the object does not refer to anything (null), then calling the equals method of this object will result in a NullPointerException. String equals is overloaded. The = = operator (in the case of primitive data types) compares the values of variables and returns the result, BUT in the case of reference data types (objects, arrays, etc.) compares references to objects in computer memory, and based on the equality or inequality of references, returns the result (true or false).
The equals()
method compared to the operation ==
The
equals
method compares characters from an object of typeString
, and the operation==
- two object references, determining whether they refer to the same object. the same instance.
The example shows two different objects of type String
, which may contain the same characters, but references to these objects when comparing will not be equal to:
// метод equals() в сравнении с операцией ==
public class EqualsNotEqualTo {
public static void main(String[] args) {
String s1 = "Сравнение";
String s2 = new String(s1);
System.out.println(s1 + " равно " + s2 + " -> " + s1.equals(s2));
System.out.println(s1 + " == " + s2 + " -> " + (s1 == s2));
}
}
The s1
variable refers to an instance class String
, created by assigning it a string literal "Сравнение"
. And the object referenced by the variable s2
is created using the variable s1
as the initializer.
So the contents of both objects of type String
are the same, but they are different objects. Therefore, the variables s1
and s2
refer not to the same object, so they are not equal, in comparison to the operation ==
.
Result:
Сравнение равно Сравнение -> true
Сравнение == Сравнение -> false