Hi, I executed the following code and got the output as : success
I could not understand the output.since we call the equals method of the object class, which is not overriden.It should return Failed.But why does it gives success as output.could anyone explain me clearly about the equals.
public static void main(
String[] args) {
String s = new String("hi");
Object o = new String("hi");
if(o.equals(s)) {
System.out.println("success");
} else {
System.out.println("Failed");
}
}
If the lines has been
String s1 = new String("hi");
String s2 = new String("hi");
then it means only one string obj is created and both s1 ans s2 refer to it.so equals shud retruns true since it has overridden.
But in the above example, we call the equals on object class which does not override equals..so actually two objects are created.but how come it retirns that they are equal?
Thanks,
Sumi