Return a fallback when a reference is null, in one readable call.
String input = null;
String value = Objects.requireNonNullElse(input, "default");
System.out.println(value);
default
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-26