Reputation: 2424
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?
Upvotes: 24
Views: 1212
Reputation: 213261
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
toString
method of the referenced object with no arguments; but if the result of invoking thetoString
method isnull
, then the string"null"
is used instead.
and concatenation will be done as - s1 + "null";
Upvotes: 28
Reputation: 4314
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
.
Upvotes: 9
Reputation: 13561
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.
Upvotes: 2
Reputation: 279970
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.
Upvotes: 15
Reputation: 22171
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
append
method
Thus, at least the left operand must not be null since StringBuffer.append(Object object)
accept Object
(including null
) as parameter.
Upvotes: 3