• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

query regarding hashcode and '=='

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic