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: 32689
4
posted
0
What sort of Exception do you get?
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
posted
0
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: 32689
4
posted
0
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: 32689
4
posted
0
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
posted
0
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
posted
0
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).
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
posted
0
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
posted
0
This is my updated code? but not sure
Chetan Raju
Ranch Hand
Joined: Aug 02, 2006
Posts: 109
posted
0
return value==((INTEGER)obj).value
Instead use
Ken Blair
Ranch Hand
Joined: Jul 15, 2003
Posts: 1078
posted
0
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
posted
0
Originally posted by Chetan Raju:
Instead use
That won't even compile, they haven't declared an intValue() method.