0

Maybe there is a reason I don't know, but I see it's being used in my code to calculate hashcode of a complex object. Does it provide anything comparing to putting Integer itself there? (I hope not), or it's just for a better clarity?

class SomeClass() {
private Integer myIntegerField1;
private Integer myIntegerField2;
...
public int hashCode() {
 final int prime = 31;
 int result =1;
 result = prime * result + ((myIntegerField1 == null) ? 0 : myIntegerField1.hashCode());
 result = prime * result + ....
 ...
 return result;
}
}
Chandrayya G K
8,8795 gold badges43 silver badges70 bronze badges
asked Dec 20, 2013 at 8:56
1
  • 3
    You'll really have to post the relevant code. Commented Dec 20, 2013 at 8:58

5 Answers 5

8

The javadoc of Integer.hashCode() says:

Returns: a hash code value for this object, equal to the primitive int value represented by this Integer object.

So using Integer.hashCode() or Integer.intValue(), or using auto-unboxing leads to exactly the same value.

answered Dec 20, 2013 at 9:00
4
  • Yes, that's why I asked the question, it really puzzled me why then hashCode() is used there. The only answer I have is for clarity. Commented Dec 20, 2013 at 9:03
  • 1
    The code is probably auto-generated by the IDE, and it uses the hashCode of the field, whatever the type of the field is. Commented Dec 20, 2013 at 9:03
  • Oh, yes, it's probably Eclipse's work. But thanks for clarifying. Commented Dec 20, 2013 at 9:05
  • @dhblah And consistency, there is little to be gained by knowing the internal implementation of each object 's hashCode just in case it doesn't need to be called. Commented Dec 20, 2013 at 10:18
4

Your posted code was auto-generated by an IDE. The code generator has no special cases to handle Integer or other primitive type wrappers, and there isn't a really good reason for it to have one: the way it is implemented now is 100% by the book and on a general level of consideration is the right thing to do.

If you replaced myIntegerField1.hashCode() with just myIntegerField1, the real effect would be a change from a hashCode() call to an intValue() call, and if you check out the source code, you'll find that these two methods are exactly the same.

answered Dec 20, 2013 at 9:06
1

Composite objects can use combined hashes of their internal state to calculate their own hash code. Example:

public class Person
{
 private Integer id;
 private String name;
 @Override
 public int hashCode()
 {
 int hash = getClass().getName().hashCode();
 if (id != null)
 {
 hash ^= id.hashCode();
 }
 if (name != null)
 {
 hash ^= name.hashCode();
 }
 return hash;
 }
}

Don't make hashes overly complicated, and base hashes only on some values which don't change, or are otherwise likely to be stable. Hash codes, by their very nature, are not required to be unique or collision-free.

The hash code is just a quick and dirty finger print that allows for a quick determination whether two instances are NOT equal (if they were equal, they would have to have the same hash code), so the actual equals() check has to be executed only for instances whose hash is equals (again, same hash does NOT imply that they are equal).

answered Dec 20, 2013 at 9:08
0

There is no reason to explicitly use the hashcode of an Integer. The source code just returns the value of the Integer:

public int hashCode(){
 return value;
}

So use the value of the Integer rather than the hash code.

What is the reason why this method is included in the source? What would happen if you had an Object that points to an Integer? Explicitly including the method in the source code ensures proper results.

answered Dec 20, 2013 at 9:00
0

Here you are trying to find the hashcode of SomeClass type objects.

public int hashCode() {
 final int prime = 31;
 int result =1;
 result = prime * result + ((myIntegerField1 == null) ? 0 : myIntegerField1.hashCode());
 result = prime * result + ....
 ...
 return result;
}

In

result = prime * result + ((myIntegerField1 == null) ? 0 : myIntegerField1.hashCode());

you are trying to check if myIntegerField1==null, return hashCode as 0 else hashCode of Integer myIntegerField1.

Remember : myIntegerField1.hashCode() and myIntegerField1.intValue() will return same value as myIntegerField1.

answered Dec 20, 2013 at 9:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.