This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes overriding equals method in my class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "overriding equals method in my class" Watch "overriding equals method in my class" New topic
Author

overriding equals method in my class

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
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 !



A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
What sort of Exception do you get?
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
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
Sheriff

Joined: Oct 13, 2005
Posts: 38467
    
  23
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.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
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.


all events occur in real time
Ramen Chatterjee
Ranch Hand

Joined: Apr 27, 2006
Posts: 62
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


Could try harder
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Apr 16, 2005
Posts: 689
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

Joined: Apr 16, 2005
Posts: 689

This is my updated code?
but not sure
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
return value==((INTEGER)obj).value


Instead use

Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Chetan Raju:


Instead use



That won't even compile, they haven't declared an intValue() method.
Chetan Raju
Ranch Hand

Joined: Aug 02, 2006
Posts: 109
sorry for that though... i that'll wake me up
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: overriding equals method in my class