Java Programming - Java.lang Class - Discussion

Discussion Forum : Java.lang Class - Finding the output (Q.No. 27)
27.
What will be the output of the program?
String s = "hello"; 
Object o = s; 
if( o.equals(s) )
{
    System.out.println("A"); 
} 
else
{
    System.out.println("B"); 
} 
if( s.equals(o) )
{
    System.out.println("C"); 
} 
else
{ 
    System.out.println("D"); 
}
  1. A
  2. B
  3. C
  4. D
1 and 3
2 and 4
3 and 4
1 and 2
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
11 comments Page 1 of 2.

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

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

Sonu said:   8 years ago
Because equals() method compares with the content of the object other than the hash code of that particular objects.

Uwe said:   8 years ago
@All.

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?

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

Ankit said:   1 decade ago
Because s is a string object and 'o' and 's' refer to same object that's why output is 'A' and 'C'.

Amey said:   1 decade ago
Here, 'o' and 's' refers to the same object. Hence, o.equals(s) ,s.equals(o) evaluates to true.

Srikanth said:   1 decade ago
.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.

Drishti said:   1 decade ago
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.

Mammu said:   1 decade ago
Please anybody explain that logic.


Post your comments here:

Your comments will be displayed after verification.