0

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?

Display Name
  • 1,121
  • 2
  • 11
  • 14

2 Answers2

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