I have a following Question i have two empty Objects and i am using equals() method to compare them, as we know equals method compare contents of an Object, but in this case i dont have any properties to my Object
Object ob1 = new Object();
Object ob2 = new Object();
if(ob1.equals(ob2)){
System.out.println(" they are Equal");
}
else{
System.out.println("not equal");
}
if(ob1 == ob2){
System.out.println(" they are Equal");
}
else{
System.out.println("not equal");
}
we know == will compare the references of the Objects
what will equals method compare here ??
thanks in Advance ...
equals()
doesn't compare the contents of the objects as you think it does. It compares objects using the implementation ofequals()
in the class of the object on which it's invoked. And thisequals()
method can do various different things depending on the type of the object. In the case ofObject
,equals()
considers two instances equal if they are the exact same instance.==
operator. Look atObjest.equals
sources.