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.
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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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'.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
