Object o1 = new Object();
Object o2 = new Object();
//o1=o2;
System.out.println(o1.equals(o2));
It returns false
. It can return true
equals method needs to be overridden inside the class if you want to make it behave in some other way. By default, it checks if two references refer to the same object.
equals
for Object
compares memory references.
That is why it is false since they are different Object
s
equals
for String
is overridden to compare based on characters.
You have 2 empty String
objects that is why equals
returns true
.
The equals()
Method of the Object
class doesn't know how to compare Strings, it only knows how to compare objects. For comparing strings, a string class will override the equals()
Method and compare strings in it.
Object.equals()
will compare only references, where String.equals()
will compare values.
The equals
implemented in the Object
class only compare references. Here is the source code:
public boolean equals(Object obj) {
return (this == obj);
}
Interesantly unlike the Sting
the StringBuffer
does not override the equals()
method. Its functionality is the same as for the Object
class.
Because equals() for String compares the content, not the object itself.
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a
String
object that represents the same sequence of characters as this object.
/* String.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;
}
(Link to the source of String.equals())
Versus the equals for Object:
The equals method for class
Object
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesx
andy
, this method returns true if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).
/* Object.equals() */
public boolean equals(Object obj) {
return (this == obj);
}
(Link to the source of Object.equals())
Also, don't forget the contract of the equals()
function:
The equals method implements an equivalence relation on non-null object references:
- It is reflexive: for any non-null reference value
x
,x.equals(x)
should return true.- It is symmetric: for any non-null reference values
x
andy
,x.equals(y)
should return true if and only ify.equals(x)
returns true.- It is transitive: for any non-null reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
.- It is consistent: for any non-null reference values
x
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or consistently returnfalse
, provided no information used in equals comparisons on the objects is modified.- For any non-null reference value
x
,x.equals(null)
should returnfalse
.
Also recommended reading: