• 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

Problem with hashcode and size

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following example out of my book and implemented this in a main class in eclipse:




Now the result in the book is:

- when hashcode method is commented the size is 3
- when hashcode method is uncommented the size is 2

But when i try this in eclipse, it gives me two times the output 3

What's right? Is the book wrong?
 
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
I think that Eclipse didn't notice the change. You have to rebuild all.
Without overriden hashcode, you use hashcode from Object, which returns always distinct values so every object is considered different - equals is not even invoked.
 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Override the hashCode() instread of writing your own method hashcode(). If you notice carefully "C" is in capital in original metod.
 
Lucas Smith
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
Yeah - I have not looked carefully - you did not override hashCode() method - you just defined distinct method which had no common things with hashCode() from Object
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The @Override annotation exists to help you catch mistakes like writing hashcode() instead of hashCode(). If you would have used it, like this:

then you would have gotten a compiler error, telling you that hashcode() does not override any method in a superclass.

By adding @Override to a method, you tell the compiler that the method is supposed to override a superclass method - and if it doesn't, the compiler will complain.

Annotations are not part of the SCJP exam, but it's useful to know them ofcourse.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also use == to compare Strings only if you want to check about String pool otherwise use equals method. Your equals method is using == for string comparison...
 
My cellmate was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic