Java Programming - Java.lang Class - Discussion
Discussion Forum : Java.lang Class - Finding the output (Q.No. 6)
6.
What will be the output of the program?
public class Test178
{
public static void main(String[] args)
{
String s = "foo";
Object o = (Object)s;
if (s.equals(o))
{
System.out.print("AAA");
}
else
{
System.out.print("BBB");
}
if (o.equals(s))
{
System.out.print("CCC");
}
else
{
System.out.print("DDD");
}
}
}
Discussion:
16 comments Page 1 of 2.
Naresh said:
1 decade ago
Both s and o are referring to same content.
So s.equals(o) and o.equals(s) will become true.
Hence AAACCC printed
So s.equals(o) and o.equals(s) will become true.
Hence AAACCC printed
Shah Ishan V said:
1 decade ago
Here, s and o both are referring to the same object.
When encounter the line Object o = (Object) s;
So, Output is : AAACCC.
When encounter the line Object o = (Object) s;
So, Output is : AAACCC.
Rohit Kshirsagar (Rx4) said:
1 decade ago
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
AAACCC.
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
AAACCC.
Himanshu Modak said:
1 decade ago
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
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:
1 decade ago
Very nice example to test the knowledge of inheritance and method overridding.
E.DIVYA said:
1 decade ago
Here s and o refers the same value so s.equal (0) & o.equal (s) are true.
The answer will be AAACCC.
The answer will be AAACCC.
Appala naidu.Marisarla said:
1 decade ago
Equals (-) method checking only the content of specified object is same as the passed parameter (or) not,
So that out put is AAACCC.
So that out put is AAACCC.
Suraj jaiswal said:
1 decade ago
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".
Now,
if(s.equals (o))
Here jvm cast object "s" and "o" in same type and then check the content which is true.
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".
Now,
if(s.equals (o))
Here jvm cast object "s" and "o" in same type and then check the content which is true.
Sambasivarao meduri said:
1 decade ago
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.
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:
1 decade ago
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)).
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers