requireNonNullElse, equals, hash and toString from java.util.Objects handle nulls correctly so you do not have to guard every call.
String missing = null;
System.out.println("requireNonNullElse = " + Objects.requireNonNullElse(missing, "fallback"));
System.out.println("Objects.equals(null, null) = " + Objects.equals(null, null));
System.out.println("Objects.toString(null, -) = " + Objects.toString(missing, "-"));
System.out.println("isNull / nonNull = " + Objects.isNull(missing) + " / " + Objects.nonNull(missing));
System.out.println("hash of two fields = " + (Objects.hash("a", 1) == Objects.hash("a", 1)));
requireNonNullElse = fallback
Objects.equals(null, null) = true
Objects.toString(null, -) = -
isNull / nonNull = true / false
hash of two fields = true
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-30