String.valueOf(null);
- why
valueOf(char[] c)is called and why notvalueOf(Object o);?? - Why
String.valueOf(null);produces aNullPointerExceptionandString.valueOf((Object)null);do not produce any exception?
On
Whenever more than one overloaded methos would be a possible target the most specific one possible would be used.
So if you pass in a char[] then valueOf(char[]) and valueOf(Object) would be possible, but valueOf(char[]) is more specific. Therefore that one will be called.
Now null is kind-of strange because it's a legal value for every non-primitive type, so it could be an argument to any of those methods. And still valueOf(char[]) is more specific than valueOf(Object), therefore the first one will be called.
String.valueOf((Object) null)calls the following method:As you can see, the null case is managed.
String.valueOf(null)calls the following method:Which itself calls: