A simple, short question: does object.hashCode()
and Objects.hashCode(object)
equal? What is the difference? Do they count the same hash from the objects?
Asked
Active
Viewed 126 times
0

Display Name
- 1,121
- 2
- 11
- 14
-
1Search before asking please – Hovercraft Full Of Eels Aug 12 '16 at 12:51
2 Answers
4
As you can see in implementation of it
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
yes. It prevents from NPE if object o is null.

ByeBye
- 6,650
- 5
- 30
- 63
3
Objects.hashCode(object)
will return zero if object
is null
.
Whereas a NullPointerException
would be thrown for object.hashCode()
in that case.
For a non-null
reference, the functions are equivalent.

Bathsheba
- 231,907
- 34
- 361
- 483