What's the difference between these two code snippets?
Snippet 1:
Object o = new Object();
int i = Objects.hashCode(o);
Snippet 2:
Object o = new Object();
int i = o.hashCode();
The only difference is that if o is null, |
|||||||||||||
|
It returns the hash code of a not-null argument and 0 for
null argument. This case it is
Returns the hashCode() of |
|||
|
This is a NPE safe alternative to o.hashCode(). No difference otherwise. |
|||
|
This is how
If |
|||
|
java.util.Objects
since 1.7. – 卢声远 Shengyuan Lu Apr 24 at 8:52