Compare labels when a reference is missing

A parcel scanner compares an expected label with a scanned label. Either reference can be null. A null-safe comparison handles missing references without throwing an exception.


The programmer uses Objects.equals from java.util.Objects to compare the labels safely while preserving String value equality when both references contain strings.


Java
import java.util.Objects;

static boolean sameLabel(String expected, String actual) {
    return Objects.equals(expected, actual);
}


Complete the results: sameLabel(null, null) returns , while sameLabel(null, "BX-42") returns .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Object Equality Questions: