File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding hashcode " Watch "overriding hashcode " New topic

overriding hashcode

Aneek Banerjee
Ranch Hand

Joined: Jun 20, 2012
Posts: 61

I have two objects lets say object 1 is t1 and object 2 is t2. I am assigning same integer value to this two objects,which is 10.Now I am confused how can I check if the both values are same for the object or not.And the second thing will the hash codes for these two objects will be same?(I believe not as ti.equals(t2) is giving me false).how can I check for the identical values here.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

When you're saying you're assigning the same integer value to two objects, what do you mean? Do you mean that the object has an integer member variable, and you're assigning the number to that?

If so, then you'd check for equality by comparing those member variables. And if you've got two different objects, and you haven't overridden hashCode(), then they will probably have different hash codes. If you have overridden hashCode(), then it depends on how you've done that.
I agree. Here's the link:
subject: overriding hashcode
It's not a secret anymore!