AlgoInsight

Q&A

How to print null in Java

By AlgoInsight Team2026-06-07stringsjava

Overview

Java's print/println methods and the string-concatenation operator are designed to handle null gracefully for object references by substituting the literal text "null". This is implemented in String.valueOf(Object), which returns "null" when given a null reference. But there are traps where null causes a NullPointerException instead of printing.

graph TD
    A[Print null] --> B["println(Object) → 'null' ✓"]
    A --> C["\"x=\" + obj → 'x=null' ✓"]
    A --> D["println((char[]) null) → NPE ✗"]
    A --> E["nullRef.toString() → NPE ✗"]
    style B fill:#1e1b4b,color:#fff
    style D fill:#6366f1,color:#fff

The Safe, Normal Cases

println / print of a null object

String s = null;
System.out.println(s);          // prints: null
Object o = null;
System.out.println(o);          // prints: null

println(Object x) internally calls String.valueOf(x), which returns the string "null" for a null argument - no exception.

String concatenation

String name = null;
System.out.println("Name: " + name);   // prints: Name: null

Concatenation also routes through String.valueOf, so null becomes "null".

Explicit conversion

System.out.println(String.valueOf((Object) null));   // "null"
System.out.println(Objects.toString(null));           // "null"
System.out.println(Objects.toString(null, "N/A"));    // "N/A" (with default)

The Traps (where null throws)

1. The char[] overload

println has an overload println(char[] x) that does not tolerate null:

char[] arr = null;
System.out.println(arr);   // throws NullPointerException!

This is a famous gotcha: the compiler picks println(char[]) over println(Object) for a char[]-typed null, and that overload dereferences the array.

2. Calling a method on null

String s = null;
System.out.println(s.toString());   // NPE - you dereferenced null
System.out.println(s.length());     // NPE

The print isn't the problem - invoking a method on a null reference is.

3. Ambiguous null literal

System.out.println(null);   // ✗ compile error: ambiguous (Object vs char[] vs String)
System.out.println((Object) null);   // ✓ cast resolves it → prints "null"
System.out.println((String) null);   // ✓ prints "null"

A bare null literal can be ambiguous between overloads; cast it to resolve.

Printing null fields inside objects

A class's toString() will show null for unset reference fields if it concatenates them:

record User(String name, String email) {}
System.out.println(new User("Ada", null));
// User[name=Ada, email=null]

Why This Matters in Interviews

This deceptively simple question tests deep knowledge: that println(Object) and + use String.valueOf (hence "null"), the println(char[]) NPE trap, why a bare println(null) can be a compile-time ambiguity, and the difference between printing a null reference (safe) and dereferencing one (NPE). The char[] gotcha is a classic "gotcha" interview question.

Common Pitfalls

References

More answers