permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals() and ==, doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "equals() and ==, doubt" Watch "equals() and ==, doubt" New topic
Author

equals() and ==, doubt

Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi,


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


Could anybody please give any example of such.

Thanks,


cmbhatt
sharan vasandani
Ranch Hand

Joined: Feb 22, 2007
Posts: 100
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.
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
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 ]
megha joshi
Ranch Hand

Joined: Feb 20, 2007
Posts: 206
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 20, 2007
Posts: 206

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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Feb 28, 2007
Posts: 1707
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 ]
K S HEGDE
Greenhorn

Joined: Apr 21, 2007
Posts: 5
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
madhu v pe
Ranch Hand

Joined: Apr 21, 2007
Posts: 100
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

Joined: Feb 28, 2007
Posts: 1707
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

Joined: Apr 21, 2007
Posts: 100
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

Joined: Apr 21, 2007
Posts: 100
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals() and ==, doubt
 
Similar Threads
hashCode Question
Satou kurinosuke i have one more question for you
|| operand
operator orders confusing
simple doubt - Java logic using false and true