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.
public final class Objects
Try it
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.
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
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 inequalsandhashCode. requireNonNullreturns its argument, so you can writethis.name = Objects.requireNonNull(name, "name");. With aSupplierthe message is built only when needed.- The null check is a precondition. The exception it throws is
NullPointerException(notIllegalArgumentException), by convention. requireNonNullElsethrows if both the value and the default arenull.Objects.equals(a, b)callsa.equals(b): order matters for classes with asymmetricalequals, though a correctequalsis symmetric.- Records generate
equals,hashCodeandtoStringfor you; useObjectsfor ordinary classes. - Related to
Optional: useOptionalfor return values that may be absent andObjects.requireNonNull*for parameters and fields.