Discussion :: Java.lang Class - Finding the output (Q.No.6)
|Naresh said: (Dec 6, 2010)|
|Both s and o are referring to same content.
So s.equals(o) and o.equals(s) will become true.
Hence AAACCC printed
|Shah Ishan V said: (Dec 10, 2010)|
|Here, s and o both are referring to the same object.
When encounter the line Object o = (Object) s;
So, Output is : AAACCC.
|Rohit Kshirsagar (Rx4) said: (Dec 20, 2010)|
|See, here s is typecast into Object
Object o = (Object)s;
which data come from s they typed into s
then, o.eqaule(s) is true
and every class in java instance of Object
then s.equals(o) thats also true
then output will
|Himanshu Modak said: (Dec 27, 2010)|
|All class inherits Object Class implicitly and as
Object o=s; or Object o=(Object)s;
Type cast the String class variable to Object class implicitly so s.equals(o), o.equals(s) are true
|Ankit said: (May 24, 2011)|
|Very nice example to test the knowledge of inheritance and method overridding.|
|E.DIVYA said: (Dec 18, 2012)|
|Here s and o refers the same value so s.equal (0) & o.equal (s) are true.
The answer will be AAACCC.
|Appala Naidu.Marisarla said: (Apr 23, 2013)|
|Equals (-) method checking only the content of specified object is same as the passed parameter (or) not,
So that out put is AAACCC.
|Suraj Jaiswal said: (Nov 6, 2013)|
|AS it is property of "equals" method that when two object to be compared using "equals" then.
1. If the two object are same type then, (in case of "string" class and "wrapper" class) it check the content. If content are same then it return "true" otherwise return "false".
2. If the two object are different then it try "cast" the two object in same type. If then can't be cast in same type then return "false" if the can be cast then check the content. If content is same then return "true" otherwise return "false".
Here jvm cast object "s" and "o" in same type and then check the content which is true.
|Sambasivarao Meduri said: (Mar 11, 2015)|
|When you use == sign between two things, this is compare to the reference address equal or not. It means both variables having same data with same.
Reference address. If you use equals, it compare data values equal or not. In the above both are true. If == is true then equal is also equal data.
|Dilip Kumar said: (Aug 28, 2015)|
|String "s" is converted into an object O in which assign same value of String i.e. "fOO" that's why (s.equals(o) or o.equals(s)).|
|Belete said: (Jan 11, 2016)|
|Very interesting and motivation questions for Java programming.|
|Janani Santhanakumar Raju said: (Nov 24, 2016)|
|Its is obvious that both s and o's reference are same. In If conditions, it is given s.equals(o) which means to check whether both are referring to the same object. Since this condition is true it prints "AAA" and same way with the o.equals(s) so it prints "CCC".|
|Alexander said: (Feb 12, 2017)|
|At o.equals(s) Object class equals method is executed & inside this equals method if both references i.e o[default "this" inside non static equals() method] & s refer to same object then 'true' is returned. thus;
sop("CCC") is executed.
At s.equals(o) , String class equals() method is executed, inside this equals method 1st code checks whether both references refer to same object see(https://dzone.com/articles/how-string-equals-method-works), hence true is returned & sop("AAA") is executed.
|Seema said: (Jul 13, 2017)|
|Yes, I too got the same output, i.e AAACCC.|
|Adam Prog said: (Nov 13, 2017)|
|I have 2 comments:
1.A super class that is casted to a sub class is not fully "equals" because the static members remain of the super class. So if the static members of the subclass are needed, its not enough to check equals (or == operator).
2.The output is the same even when comparing with another string that only its value is the same (but in such a case operaton == returns false), since o object points now to the equals method of the instance of String that doesn't care about addresses difference :
String s1 = new String("foo");
String s2 = new String("foo");
Object o = (Object)s1;
|Lahul said: (Dec 30, 2017)|
|Correct answer is B.
Because object class equals method compares address whereas in String class its overridden to compare the content.
So o.equals (s) will be false.
Post your comments here:
Email : (optional)
» Your comments will be displayed only after manual approval.