JavaChallenge40
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
(orStringBuffer
) class and itsappend
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 —