class Test {
public static void main() {
String s1 = null + null; //shows compile time error
String s1 = null;
String s2 = s1 + null; //runs fine
}
}
Can anybody please explain the reason for this behavior?
This code:
String s1 = null + null;
tries to perform addition operation on two null, which is not valid.
while here:
String s1 = null;
String s2 = s1 + null;
You assigned null to s1. Then you perform concatenation of s1 and null. The type of s1 is String, so null in s1 + null will be converted to "null" string as per the String conversion rules, as in JLS §15.1.11 - String Conversion:
If the reference is null, it is converted to the string
"null"(four ASCII charactersn, u, l, l).Otherwise, the conversion is performed as if by an invocation of the
toStringmethod of the referenced object with no arguments; but if the result of invoking thetoStringmethod isnull, then the string"null"is used instead.
and concatenation will be done as - s1 + "null";
a + b + c. Because + is left-associative, the expression is parsed as (a + b) + c. If it were right-associative, the expression would be parsed as a + (b + c). (It's also possible for an operator to be non-associative, in which case the expression would either be a syntax error or have some kind of special meaning, depending on the syntax definitions.)"null", will it? I thought it would just stay as a null pointer.s1 + null addition, not the s1 = null assignment.s1 of type String, in s1 + null? There is no type for null, when used directly, but when it's assigned to String s1, the operator will see the type of s1, and not that whether it is null or not.The + operator as String concatenation only applies if one (or both) of the operands has type String.
This is defined in the Java Language Specification
If the type of either operand of a
+operator isString, then the operation is string concatenation.
In
String s1 = null + null; //shows compile time error
the operands have the null type, ie. not String, so no string concatenation happens. Java then thinks you are doing an addition which also doesn't work on null types.
In
String s2 = s1 + null; //runs fine
s2 has type String even though it is referencing null, so string concatenation can happen.
In below case, you are performing operation on two null and + operation is not defined for two null.
String s1 = null + null;
And in this one you are performing concatenation of String with null.
String s2 = s1 + null;
And another interesting case is,
String abcd = (String) null + null;
That will be resulted in "nullnull" string as you are casting String to null and again concatenation will be performed. so first null will be treated as String.
From Javadoc:
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
appendmethod
Thus, at least the left operand must not be null since StringBuffer.append(Object object) accept Object (including null) as parameter.
append(null) - it complains that it's ambiguous. Not sure what to make of that.append((String)null) in order to be able to select the right overloaded method. On the "concept" aspect, it accepts null, technically, a cast is necessary.Basically this because you are dealing with literals, and because of that, the COMPILER is trying to perform an operation on those literal values. It's happening at compile time, and there's nothing to do with two null values.
The compiler error you are getting is the equivalent of an exception -- the compiler doesn't know what to do with it, so it errors out and gives the types an explanation saying it doesn't work.
concatwould be invoked with a null.s1is declared twice. Maybe use different scopes by putting the two examples in different blocks?