5

Only interesting, why method hashCode() in java.lang.String is not static? And in case of null return e.g. -1 ? Because frequently need do somethihg like:

String s;
.............
if (s==null) {
  return 0;}
else {
  return s.hashCode();
}

Thanks.

4
  • It wouldn't be a very useful hash if static. (And note that hashCode is a method of Object.) Commented Aug 8, 2011 at 12:20
  • (You can, of course, write your own static myHash method that checks its parameter for null.) Commented Aug 8, 2011 at 12:21
  • 1
    static int hashCodeOf( Object o ) { return (o != null) ? o.hashCode() : 0; } Commented Aug 8, 2011 at 12:26
  • 1
    @oliholz: and if you do it, make it accept an Object. Commented Aug 8, 2011 at 12:28

5 Answers 5

15

As others have noted hashCode is a method on Object and is non-static because it inherently relies (i.e. belongs to) an object/instance.

Note that Java 7 introduced the Objects class, which has the hashCode(Object) method, which does exactly what you want: return o.hashCode() if o is non-null or 0 otherwise.

This class also has other methods that deal with possibly-null values, such as equals(Object, Object), toString(Object) and a few others.

Sign up to request clarification or add additional context in comments.

2 Comments

" return o.hashCode() if o is non-null or 0 otherwise." How can you call hashCode() on a null and get 0?
@HelenCui: you can't call hashCode on null, obviously. That's why you call the static method on the class called Objects and pass the possibly-null value in: int hash = Objects.hashCode(aVariableThatMightBeNull). If the variable was null then hash will be 0, otherwise it will be the value returned by its hashCode() method.
8

because if it was static "1".hashCode() and "2".hashCode() would have returned the same value, which is obviously wrong.

It is specific per instance, and influenced by it, therefore it cannot be static.

3 Comments

Wrong: if a static hashCode(String) was provided along the non-static String.hashCode(), it could be called like this String.hashCode("1"), or, if you ignore the warning, "2".hashCode("1"). Both calls would return the same value.
@bernardpaulus But the questions is not about hashCode(String), it's about hashCode() (unparametrized).
a no-parameter static String.hashCode() does not make much sense (should not even compile as it cannot override Object.hashCode() ). Plus the foo() notation is often used to indicate that foo is a function, without specifying the parameters.
2

Because the hash code of a String is a property of that String.

With the same train of thought you could make every method static.

Comments

2

hashCode is used to get the hashCode of an object, in order to know in which bucket of a HashMap this object must be placed. It thus has to be an instance method of the object, and it must be called polymorphically.

null can be used as a key in a HashMap, but it's treated as a special case.

You seem to be using hashCode for a different purpose, so you have to handle is in a specific way.

Comments

1

Its returning hashCode of an Object not an class.

Comments

Your Answer

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.