Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Problem with hashcode and size

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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...
 
If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic