• 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

Attempt to override equals() & hashCode() - please point out error(s)!

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The hashCode() method is based on a Whizlabs example, the rest is not.
Neither equals nor hashCode appear to be overriding the java.lang.object methods, and I do not know why. The signature is the same.

The output is this, which shows that the default methods are still being used?




Constructive comments only please. Thank you.
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nigel Shrin wrote:The hashCode() method is based on a Whizlabs example, the rest is not.
Neither equals nor hashCode appear to be overriding the java.lang.object methods, and I do not know why. The signature is the same.


No - hashCode(Object) is different from hashCode(). You're not overriding unless the number and types of the parameters are the same. One has no parameters, while the other has a single Object parameter. Not the same.
 
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And your overriden equals method expects an object of type 'Q64_hashCode_intArray' (not Integer[]).
 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is my first attempt at creating my own equals() & hashCode(); it is closer but still not quite right, I realised I need to pass an object and cannot use it to compare arrays within an object.
This is closer, but the last two lines return (equals) TRUE when they should be FALSE, can someone highlight my error please?



thank you very much
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also want to check out #28 in the JavaBeginnersFaq.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is this used for ?
 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What is this used for ?
if(this.arr1.length==5){



I wanted to say this object is equals if the array has five elements and their sum comes to 15, just as an experiment in learning to use equals() and hashCode(). The array is obviously is declared as 5, but it could be a stringBuffer etc. It has no purpose beyond trying to understand the implementation.





 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:You might also want to check out #28 in the JavaBeginnersFaq.



Hi Ulf,
thanks for the links, I bought the Effective Java book after reading them.
reply
    Bookmark Topic Watch Topic
  • New Topic