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");
}
- A
- B
- C
- D
Discussion:
11 comments Page 1 of 2.
Hrishi said:
2 decades ago
I want to learn basic of java upto multithreading.
Mammu said:
1 decade ago
Please anybody explain that logic.
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.
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.
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.
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.
Amey said:
1 decade ago
Here, 'o' and 's' refers to the same object. Hence, o.equals(s) ,s.equals(o) evaluates to true.
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'.
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.
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.
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?
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?
Sonu said:
8 years ago
Because equals() method compares with the content of the object other than the hash code of that particular objects.
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.
Object equals method doesn't check the return value of the other object's equals method.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers