• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • 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.
 
Dennis Grimbergen
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might also want to check out #28 in the JavaBeginnersFaq.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is this used for ?
 
Nigel Shrin
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic