Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Objects class

Reference for java.util.Objects: null-safe equals, hash, toString, requireNonNull and requireNonNullElse, index checks, and writing correct equals/hashCode.

java.util.Objects is a final utility class with static methods for the chores every class has to do: comparing objects without worrying about null, computing hash codes, producing strings, and validating arguments. It was added in Java 7 and extended in Java 9 (requireNonNullElse, index checks) and later releases.

Its most common uses are inside your own classes: Objects.equals(a, b) and Objects.hash(...) for equals() and hashCode(), and Objects.requireNonNull(x, "message") at the top of constructors and public methods to fail fast with a clear error.

Java
public final class Objects

Try it

Java Null-safe comparison, hashing and strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java A correct equals/hashCode and fail-fast validation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
Objects.requireNonNull(T obj, String message)
Objects.equals(Object a, Object b)
Objects.hash(Object... values)
Method Description
boolean equals(Object a, Object b) true if both are null or a.equals(b).
boolean deepEquals(Object a, Object b) Like equals, but compares arrays by contents (also nested).
int hashCode(Object o) o.hashCode(), or 0 for null.
int hash(Object... values) Combined hash of the values (Arrays.hashCode); hash() is 1, hash((Object) null) is 31.
String toString(Object o) / toString(Object o, String nullDefault) String.valueOf(o), or the default when o is null.
<T> T requireNonNull(T obj) / (T obj, String message) / (T obj, Supplier<String> messageSupplier) Returns obj, or throws NullPointerException.
<T> T requireNonNullElse(T obj, T defaultObj) obj if non-null, else defaultObj (which must not be null). Since Java 9.
<T> T requireNonNullElseGet(T obj, Supplier<? extends T> supplier) Lazy default. Since Java 9.
boolean isNull(Object) / nonNull(Object) Handy as method references: filter(Objects::nonNull).
<T> int compare(T a, T b, Comparator<? super T> c) 0 if a == b by identity, else c.compare(a, b).
int checkIndex(int index, int length) Returns index or throws IndexOutOfBoundsException. Since Java 9; checkFromToIndex and checkFromIndexSize also exist.

Usage notes

  • Objects.hash() boxes and allocates an array on every call. Fine for most classes; for hot-path keys, compute the hash by hand or cache it.
  • Hash values are not specified beyond the formula of Arrays.hashCode; never persist them. Equal objects must have equal hashes, so use the same fields in equals and hashCode.
  • requireNonNull returns its argument, so you can write this.name = Objects.requireNonNull(name, "name");. With a Supplier the message is built only when needed.
  • The null check is a precondition. The exception it throws is NullPointerException (not IllegalArgumentException), by convention.
  • requireNonNullElse throws if both the value and the default are null.
  • Objects.equals(a, b) calls a.equals(b): order matters for classes with asymmetrical equals, though a correct equals is symmetric.
  • Records generate equals, hashCode and toString for you; use Objects for ordinary classes.
  • Related to Optional: use Optional for return values that may be absent and Objects.requireNonNull* for parameters and fields.