Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Attempt to override equals() & hashCode() - please point out error(s)! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Attempt to override equals() & hashCode() - please point out error(s)!" Watch "Attempt to override equals() & hashCode() - please point out error(s)!" New topic
Author

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

Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
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.

Nigel
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2970
    
    9
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

Joined: Nov 04, 2009
Posts: 138

And your overriden equals method expects an object of type 'Q64_hashCode_intArray' (not Integer[]).


SCJP, SCWCD, SCJD
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
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
Marshal

Joined: Mar 22, 2005
Posts: 39578
    
  27
You might also want to check out #28 in the JavaBeginnersFaq.


Ping & DNS - updated with new look and Ping home screen widget
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

What is this used for ?


[My Blog]
All roads lead to JavaRanch
Nigel Shrin
Ranch Hand

Joined: May 18, 2009
Posts: 137
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

Joined: May 18, 2009
Posts: 137
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Attempt to override equals() & hashCode() - please point out error(s)!
 
Similar Threads
hashcode & equals - how to check array
Need some help IndexOf method...pls
Garbage collection
equals in the case of arrays
Problem faced in using hashset(comparison of equal elements)