Fundamentals 6 min read

Why Does Java Concatenating a Null String Yield “nullhello world”?

This article explains why concatenating a null String with another string in Java produces the unexpected "nullhello world" output, debunks common misconceptions, and details the compiler's StringBuilder optimization, bytecode behavior, and related null‑handling mechanisms.

Senior Brother's Insights
Senior Brother's Insights
Senior Brother's Insights
Why Does Java Concatenating a Null String Yield “nullhello world”?

Introduction

String type can be tricky; concatenating a null reference with a string often produces unexpected output like "nullhello world". The article examines why this happens and corrects common misconceptions found online.

Root Cause Analysis

Many sources claim the expression s + " world" is equivalent to String.valueOf(s) + "world", but that is inaccurate. The actual valueOf method returns the literal "null" when its argument is null, which would produce "nullworld".

Java Compiler Optimization

The Java compiler optimizes string concatenation using StringBuilder. For example, the code

String a = "Hello ";
String b = "World";
System.out.println(a + b);

is compiled to something equivalent to:

StringBuilder sb = new StringBuilder();
sb.append("Hello ");
sb.append("World");
String result = sb.toString();
System.out.println(result);

Thus the plus operator is transformed into StringBuilder operations, not a call to String.valueOf. When the left operand is null, the compiler generates

StringBuilder sb = new StringBuilder(null);
sb.append("hello");
sb.append(" world");
System.out.println(sb.toString());

Inside StringBuilder, the append method checks for null and delegates to appendNull, which appends the characters 'n','u','l','l'. This explains the "null" prefix in the result.

Bytecode Inspection

Running javap -c on the compiled class shows bytecode that matches the above analysis, confirming the compiler’s use of StringBuilder and the handling of null.

bytecode
bytecode

Extended Questions

Printing a null reference directly:

String s = null;
System.out.println(s);

calls PrintStream.print(String), which substitutes the literal "null" when the argument is null, then writes it.

When String.valueOf(Object) is invoked explicitly with a null object, it also returns the string "null". For wrapper types like Integer, System.out.println(i) eventually calls String.valueOf(i), leading to the same "null" output.

Conclusion

String concatenation involving null is governed by the compiler’s StringBuilder optimization and the null‑handling logic in appendNull. Understanding this mechanism helps avoid misconceptions when searching for solutions online.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavacompilerbytecodeStringNULLstringbuilder
Senior Brother's Insights
Written by

Senior Brother's Insights

A public account focused on workplace, career growth, team management, and self-improvement. The author is the writer of books including 'SpringBoot Technology Insider' and 'Drools 8 Rule Engine: Core Technology and Practice'.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.