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"); 
        } 
    } 
}
AAACCC
AAADDD
BBBCCC
BBBDDD
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
16 comments Page 1 of 2.

Adam Prog said:   8 years ago
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;
if (s2.equals(o))
{
System.out.print("AAA");
}
else
{
System.out.print("BBB");
}
if (o.equals(s2))
{
System.out.print("CCC");
}
else
{
System.out.print("DDD");
}

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.

Alexander said:   9 years ago
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.

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.

Janani Santhanakumar raju said:   9 years ago
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".

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.

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

Lahul said:   8 years ago
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.

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.

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:

Your comments will be displayed after verification.