🔤 Strings & Text · Beginner

Null-safe string handling in Java

"literal".equals(x), Objects.equals, isEmpty vs isBlank.

🧩 The mysteryOne user leaves the "nickname" field empty, and your whole page crashes with NullPointerException. The culprit is a method call on... nothing.

null means "no object"

A null reference points to no object at all. Calling any method on it — equals, isEmpty, length — throws NullPointerException, because there's nobody there to answer.

String answer = null;
answer.equals("yes");  // NPE!

Who should go first?

✗ Crashes on null
if (answer.equals("yes")) {
    ...
}

If answer is null, this throws.

✓ Null-safe
if ("yes".equals(answer)) {
    ...
}

The literal is never null, and equals(null) simply returns false.

Objects.equals: safe on both sides

When both values might be null, use Objects.equals(a, b). It returns true if both are null, false if only one is, and otherwise calls a.equals(b).

Objects.equals(null, null);   // true
Objects.equals("a", null);    // false
Objects.equals("a", "a");     // true
🔮 Predict it

Null in action

What does this print?

String a = null;
System.out.println(Objects.equals(a, null));
System.out.println("ok".equals(a));
System.out.println(a + "?");
  1. true false null?
  2. false false ?
  3. Throws NullPointerException
Show the answer

true, false, null?. Note the last one: string concatenation **turns null into the text "null"** instead of throwing.

isEmpty vs isBlank

isEmpty() is true only for length 0. isBlank() (Java 11) is also true for whitespace-only text like " ". Both are methods — so both throw on null. Check for null first.

"".isEmpty();    // true
" ".isEmpty();   // false (length 1)
" ".isBlank();   // true
⚠️ The trap

Order matters in ||

|| evaluates left to right and stops as soon as it finds true. Put the null check first, so the method call never runs on null.

// crashes: isEmpty runs on null
if (city.isEmpty() || city == null)
// safe: stops at city == null
if (city == null || city.isBlank())
💼 In the real world

In real projects

Missing form fields, optional JSON properties and empty database columns all show up as null. That's why libraries like Apache Commons offer StringUtils.isBlank(s), which returns true for null too. The habit to build: null check first, then content check.

Key takeaways

  1. "literal".equals(x) is safe when x is null
  2. Objects.equals(a, b) is null-safe on both sides
  3. isEmpty: length 0; isBlank: only whitespace (Java 11)
  4. Check x == null first: || short-circuits
🤯 Did you know?

Tony Hoare, who introduced null references in ALGOL W in 1965, later called them his "billion-dollar mistake" because of the crashes they have caused ever since.

Practice questions

What does this print?

String name = null;
System.out.println("Bob".equals(name));
System.out.println(Objects.equals(name, null));
  1. false true
  2. false false
  3. Throws NullPointerException
  4. true true
Check your answer

false true. "Bob".equals(null) returns false safely. Objects.equals returns true when both arguments are null.

What does this print?

System.out.println("".isEmpty());
System.out.println(" ".isEmpty());
System.out.println(" ".isBlank());
  1. true true true
  2. true false true
  3. true false false
  4. false false true
Check your answer

true false true. isEmpty is true only when length is 0. A single space has length 1, but isBlank treats whitespace-only text as blank.

Next world: Classes & Objects — stop only *using* Java's types and start designing your own.