This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes query regarding hashcode and '==' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "query regarding hashcode and Watch "query regarding hashcode and New topic
Author

query regarding hashcode and '=='

shubham garg
Greenhorn

Joined: Jun 22, 2009
Posts: 17
consider the following code :-

class Foo {
public static void main(String[] args) {
Foo a = new Foo();
Foo b = new Foo();
System.out.println(a);
System.out.println(b);
System.out.println(a==b);
}
public int hashCode() {
return 4;
}
}

output :-

Foo@4
Foo@4
false //line 3

'==' compares the objects on its ends by their respective refrence values (which have been assinged same by overriding the 'hashCode()' method).
so i expected a 'true' on line 3 please help me out here...

thanks and regards
shubbham


preparing for SCJP 6
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

You need to understand the hashCode() contract.
== checks if two references points to the same object.
You have used "new" keyword. You have created two different objects.
hashCode() is not even invoked in that case!


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

.equals would do what you're expecting == to do in this case.

== compares memory locations. It's like having two cars that are exactly the same type and model, but sitting in different parking spots. From a .equals perspective they are the same, but they are actually different physical cars, in two different parking spots, so == says they are different. That's sorta the difference between comparing for similarity with .equals and comparing actual locations in memory where two seeming different objects might be stored.

Check out this mock exam on .equals, = and ==. Click the "Begin Exam" button and ignore the clutter on the initial page. :P If you click and drag where it says "Don't Click and Drag" you'll see some pretty detailed answers that will really help you understand the concepts.

Practice Java Exam on .equals and == with Answers

Good luck on your SCJP Sun Java Certifications!

-Cameron McKenzie

shubham garg
Greenhorn

Joined: Jun 22, 2009
Posts: 17
Cameron Wallace McKenzie wrote:.equals would do what you're expecting == to do in this case....





thanks Cameron !! that link was really vry helpful....

but still .equals is also unable to do what i was expecting to be done...
(i think .equals of Object class and '==' are exactly same... )

thanks and regards
shubham
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

Indeed, until the .equals method is overridden, it doesn't have much meaning other than what is inherited from Object.

-Cameron McKenzie
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: query regarding hashcode and '=='
 
Similar Threads
about gc()
K&B - Erratum ?
HashCode mock question
Exception Handling
Access Modifier Confusion