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 2 of 2.
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.
Ankit said:
1 decade ago
Very nice example to test the knowledge of inheritance and method overridding.
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
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.
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.
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
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers