Utilities: Objects.equals compares two possibly-null references safely

Objects.equals checks both references for null before delegating to equals, so it never throws a NullPointerException the way a.equals(b) would when a happens to be null.

Code
String a = null;
String b = "value";
System.out.println("null vs value: " + Objects.equals(a, b));
System.out.println("null vs null: " + Objects.equals(null, null));
System.out.println("value vs value: " + Objects.equals("value", "value"));
Output
null vs value: false
null vs null: true
value vs value: true
Advertisement
More in JAVA

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-09-27

© Java Coding Hub · About · Contact · Privacy · Terms