JavaChallenge40

Govinda Raj
wolfie/JavaChallenge
1 min readSep 19, 2021

--

1. NullPointerException
2. Compile-time Error
3. nullJava Challenge

Try to solve the challenge by yourself first.

Surprisingly, It will not throw NullPointerException instead it will print nullJava Challenge . This is because all the + operations are automatically compiled to StringBuilder flows so that a+b+c (where all variables are Strings) becomes

 new StringBuilder().append(a).append(b).append(c).toString();

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.

We can see from StringBuilder.append(Object o)'s Javadoc, nulls are handled by printing null instead of throwing an exception.

Let me know if you liked the challenge.

References —

--

--

Govinda Raj
wolfie/JavaChallenge

Senior Software Developer. Tech Enthusiast and love coding. My portfolio: https://govinda-raj.github.io/