Discussion :: Java.lang Class - Finding the output (Q.No.27)
|Hrishi said: (Jun 24, 2010)|
|I want to learn basic of java upto multithreading.|
|Mammu said: (Feb 21, 2011)|
|Please anybody explain that logic.|
|Drishti said: (Aug 29, 2011)|
|public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
|Srikanth said: (Dec 16, 2011)|
|.equals() is used for content comparison in String.
So String creates one object of scp with reference of s.
Here String reference is assigns to Object.
So Object and String references are both poining to same content "hello".
The (o.equals(s))and (s.equals(o) checks content of 's' and 'o'.
The content is same it returns True.
|Amey said: (Mar 19, 2012)|
|Here, 'o' and 's' refers to the same object. Hence, o.equals(s) ,s.equals(o) evaluates to true.|
|Ankit said: (Oct 15, 2013)|
|Because s is a string object and 'o' and 's' refer to same object that's why output is 'A' and 'C'.|
|Garun Mishra said: (Jul 6, 2016)|
|1. As we know that String and Object should different object if they will declare and assign separately with the new keyword. But in this case, String object got assign to Object's reference so that they are pointing to the same object now. That's why this will return true.
2. Now s and pointing to the same object and same values.
3. o. Equals (s) will check the contents and will return true.
Note: equals () checks the value and == will checks by reference.
|Uwe said: (Mar 20, 2017)|
I've understood, that they are pointing to the same object, but Object is the superclass of String, so an Object o, which points to the same place can just "see" the part of the Stringobjectvariable of s which is "visible" for an object of type Object. So for me things are not so clear. My question is: Subclass s = new Subclass(); Superclass sc = s; s.equals(sc); // must be a general rule, Am I right?
|Sonu said: (Apr 2, 2017)|
|Because equals() method compares with the content of the object other than the hash code of that particular objects.|
|Adam Prog said: (Nov 13, 2017)|
|Drishti said: "It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true." - It's not true if x and y are not of the same type, so it is not relevant to our case.
Object equals method doesn't check the return value of the other object's equals method.
|Neha said: (Feb 27, 2018)|
|It first creates an object which has value as hello, n then its assigned to o. So both will have the same value. Now if the condition is satisfied so it won't enter else loop. Hence the answer is A and C.|
Post your comments here:
Email : (optional)
» Your comments will be displayed only after manual approval.