Object o1 = new Object();
Object o2 = new Object();
//o1=o2;
System.out.println(o1.equals(o2));
It returns false
. It can return 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.