• 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

overriding equals method in my class

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello , here i am planning to simulate equals method in my program
but unable to do it..
it is throwing an exception at runtime

can any one please correct this program

really thanks !

 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What sort of Exception do you get?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice in your equals method that you test to see if obj is a reference to a Super object. You can't cast a Super object to an Integer.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looks like a beginner's problem.

Yes, the cast Exception is one of thsoe I got when I tried your app. You need to test the two values. Try:
[ September 11, 2006: Message edited by: Campbell Ritchie ]
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You ought also to override the hashCode method.

Take every field. If it is a primitive multiply it by a prime number, if a reference type multiply its hashCode by a prime number. Add all those figures together and add to super.hashCode(); . Use different prime numbers for each field.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi cowboys,

apart from what's already mentioned:

The bold line will run before the this.s=s;
So you are actually parsing null.
A number format exception will be thrown.
So put the parsing line in the constructor and the declaration of value above.


Yours,
Bu.
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

Just to add to those comments already made. If you are overriding the equals method, you should meet the contract specified in the API documentation for java.lang.Object. If there are other meaningful tests for equality include those. This is most common in data objects

For example an employee object using the equals method from Object would only return true if 2 instances they referred to the same object. It would be preferrable to have equals return true of the instances referred to the same employee (one test may be equality on the NI/social securtiy number, or maybe the employee number, etc).

Josh Bloch's excellent book Effective Java Programming covers this and other rules.

Hope this helps

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

Originally posted by Campbell Ritchie:
You ought also to override the hashCode method.

Take every field. If it is a primitive multiply it by a prime number, if a reference type multiply its hashCode by a prime number. Add all those figures together and add to super.hashCode(); . Use different prime numbers for each field.



I don't see how using a different prime for each field is going to get you anything. Using different initial values in each class, sure, but that's another story.
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Campbell Ritchie:
You ought also to override the hashCode method.

Take every field. If it is a primitive multiply it by a prime number, if a reference type multiply its hashCode by a prime number. Add all those figures together and add to super.hashCode(); . Use different prime numbers for each field.



Thanks for your suggestion but why should i follow the same way by multiplying each number with prime number?
can i directly return the value in the hashcode method???
 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is my updated code?
but not sure
 
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

return value==((INTEGER)obj).value



Instead use

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

Originally posted by saikrishna cinux:
Thanks for your suggestion but why should i follow the same way by multiplying each number with prime number?
can i directly return the value in the hashcode method???



In your specific example, yes. He was getting at a more generic template for creating a decent hashCode() method. What he described is very similar to what Bloch suggested in Effective Java with some minor changes. Generally an object will have more than one field which is involved in comparing for equality with equals() and a hashcode needs to be generated using all of them. Since in this case you only have one field, and it is already an int, and your objects are considered equal when that field is equal, you might as well just return it. In fact, that's exactly what java.lang.Integer does.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Chetan Raju:


Instead use



That won't even compile, they haven't declared an intValue() method.
 
Chetan Raju
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry for that though... i that'll wake me up
reply
    Bookmark Topic Watch Topic
  • New Topic