-2

If x==y is true so y==x should be true isn't that the case?

But I found this statement :

Reverse is not necessary true

Please help me with this?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Mohammed
  • 31
  • 7
  • 2
    Where did you find this statement? – Maroun Dec 21 '13 at 10:05
  • `x==y` is a query for the same object reference, assuming x and y are Java objects, while `x.equals(y)` just checks the content of two different objects - and here the content can be different for two objects of the same class. – Roman Vottner Dec 21 '13 at 10:06
  • 3
    Your title and the body of the question are two different questions. You're not sure what you are asking. – Maroun Dec 21 '13 at 10:06
  • I am confused, Please answer the title question – Mohammed Dec 21 '13 at 10:09
  • @user3125031 "The reverse" meant here would be "if a.equals(b) is true for some String objects, then also a==b is true." and this would be **false** – Ingo Dec 21 '13 at 10:12
  • @MarounMaroun http://www.scribd.com/doc/188530778/0073523399-Java pg 561 – Mohammed Dec 21 '13 at 10:16

1 Answers1

5

If x and y are String objects and x == y is true, then x.equals(y) is also true.

But

If x.equals(y) is true then x == y may be false.

Consider the following example:

String x = "abc";
String y = new String("abc");
System.out.println(x == y); //false
System.out.println(x.equals(y)); //true
Konstantin Yovkov
  • 62,134
  • 8
  • 100
  • 147