• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

query regarding hashcode and '=='

 
shubham garg
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Lucas Smith
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
.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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, until the .equals method is overridden, it doesn't have much meaning other than what is inherited from Object.

-Cameron McKenzie
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic