according to the documentation, the method String.valueOf(Object obj)
returns:
if the argument is
null
, then a string equal to"null"
; otherwise, the value ofobj.toString()
is returned.
But how come when I try do this:
System.out.println("String.valueOf(null) = " + String.valueOf(null));
it throws NPE instead? (try it yourself if you don't believe!)
Exception in thread "main" java.lang.NullPointerException at java.lang.String.(Unknown Source) at java.lang.String.valueOf(Unknown Source)
How come this is happening? Is the documentation lying to me? Is this a major bug in Java?
The issue is that String.valueOf
method is overloaded:
Java Specification Language mandates that in these kind of cases, the most specific overload is chosen:
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
A char[]
is-an Object
, but not all Object
is-a char[]
. Therefore, char[]
is more specific than Object
, and as specified by the Java language, the String.valueOf(char[])
overload is chosen in this case.
String.valueOf(char[])
expects the array to be non-null
, and since null
is given in this case, it then throws NullPointerException
.
The easy "fix" is to cast the null
explicitly to Object
as follows:
System.out.println(String.valueOf((Object) null));
// prints "null"
There are several important ones:
valueOf(char[])
overload is selected!null
(examples to follow)null
There are at least two situations where explicitly casting null
to a specific reference type is necessary:
null
as a single argument to a vararg parameterA simple example of the latter is the following:
static void vararg(Object... os) {
System.out.println(os.length);
}
Then, we can have the following:
vararg(null, null, null); // prints "3"
vararg(null, null); // prints "2"
vararg(null); // throws NullPointerException!
vararg((Object) null); // prints "1"