• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

equals() and ==, doubt

 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,


If x.equals(y) is false, then x==y may be true [FALSE]



Could anybody please give any example of such.

Thanks,
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer x=new Integer(1);

Integer y=x;

now if you x.equals(y) it will be true and if you do x==y it will be true,

so if x.equals(y) is true then x==y may be true if x and y refer to same object.and x==y may be false if they refer to different objects.

but if x==y is true then x.equals(y) will always be true.if its implemented according to the contract.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Integer i=new Integer(12);
Integer i1=new Integer(12);
System.out.println(i.equals(i1));//true
System.out.println(i==i1);//false

But the reverse is not true
means
if i==i1//true
then i.equals(i1) must be true
[ May 01, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am sorry but I didnt see the code complying with Chandra's original question...so just putting it down in case anyone didnt get it...

We want to prove...

x.equals(y) = False
x == y = True

See the following code which proves it



This is becuase the == opearator allow flexiblity of widening / unwindening when comparing objects to primitives...but .equals does not.

Also now I see that the questions's answer is false. So that happens in case x and y both are objects . I guess that would be given in the complete question for the statement to be false.

Thanks!
Megha
[ May 02, 2007: Message edited by: megha joshi ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your nice response Megha,

But as you already pointed it out that the question would only be correct
(given option false) when both are objects. But it was not given as such,so
should we assume both are objects?


Thanks Anil and Sharan to stick to the true facts. I think we should generally adhere to the facts pointed by Anil and Sharan.


Thanks all,
 
megha joshi
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thanks Anil and Sharan to stick to the true facts. I think we should generally adhere to the facts pointed by Anil and Sharan.



I woundn't assume that x and y are objects if the question does not mention that both are objects.

Is it that for the purpose of the exam we have to consider only objects,
when it comes to such questions? Did K&B or any other source mention it.
What was the source of this question?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code originally posted by Megha:


I agree with this.
But, although we have flexibility of comparing object with a primitive because of the signature of the equals() method

public boolean equals(Object o) {}

anything (primitive,object,enum etc.) can be used where Object is required,
we get result "false" because IS-A fails.

And the second case is known fact that unboxing happens before comparison and we get true result.

Finally, I want to say that the correctness of the answer depends upon the question whether it is talking about both objects or not. But we should keep in mind the case pointed out by Megha too.

Isn't it?
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry you all to not mention the source of the question.

Question explicitly says, both x and y are reference variables for two
objects of class Dog.


If class Dog follows the equals the equals and hashCode contracts, and
if x and y are reference variables for two Dog objects, which statements are true? (Choose all that apply)

A. If x.equals(y) is true, then x==y must be true
B. If x.equals(y) is false, then x==y may be true
C. If x.equals(y) is true, then x==y may be false
D. If x.equals(y) is true, then (x.hashCode() == y.hashCode()) may be
false.
E. If x.equals(y) is false, then (x.hashCode()==y.hashCode()) may be true
F. If x==y is false, then x.equals(y) must be false.




* I was not willing to type the complete question as the software does not
allow to copy paste capability, what finally I had to do.

Source: K&B Bonus Exam
[ May 02, 2007: Message edited by: Chandra Bhatt ]
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friends,

"==" checks the equality of object references
equals() checks the equality of contents of the objects

Hence only option "C" is right

thank you

K S Hegde
 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Please any one can clarify the foll statement from K&B-Overrding equals() topic.



my understanding is - "it looks each bit memory in the variable while comparing with =="

Thanks
[ May 04, 2007: Message edited by: madhu v pe ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by madhu v pe:
Hi All,

Please any one can clarify the foll statement from K&B-Overrding equals() topic.



Thanks



Yeah it is fine! It means if two reference variables refer to the
same object on the heap, they will have same hashCode and identical
bit information. So if they hold identical bits they are ==, means
referring to the same object on the heap hence == returns true.
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Chandra,
please look into the below code


my explanation is
line 4 ==>
since in String pool already "abc" is existing it creates one more object str1
str4=str1 which means str4 is also pointing to str1
str1==str4 shoulb true right?

line 5==>
since str is creating an object for literal str5 won't create another one str5 also should point to str location
then it should return true right?

Note:
now I am compiling with 1.4 will it change with 1.5

Please clarify me
 
madhu v pe
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah my above answers are correct only.
the problem is with operator precedence i think
when I kept comparisons in () it is giving correct results what I expected.
it should be + will have more precedence than ==.

If I am wrong please let me know what is the point there.

Thanks
 
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic