4
String.valueOf(null);
  1. why valueOf(char[] c) is called and why not valueOf(Object o); ??
  2. Why String.valueOf(null); produces a NullPointerException and String.valueOf((Object)null); do not produce any exception?
Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • Because valueOf(char[] data) is the first method that null could be adapted to (as an Array is an Object). – Elliott Frisch Jan 13 '14 at 14:00
  • 1
    The most specific method will be called. `null` is not an Object, making `char[] c` more specific. – Maroun Jan 13 '14 at 14:04
  • @ᴍarounᴍaroun `null` *is* an Object as in being assignment-compatible with the type `Object`. – Marko Topolnik Jan 13 '14 at 14:05
  • @MarkoTopolnik I don't understand that. AFAIK, `null` is not an Object, actually it's the "absence" of an Object. – Maroun Jan 13 '14 at 14:07
  • OP, as for your 2., would a Javadoc quote satisfy you, or do you want to pry open JDK API designers' heads? – Marko Topolnik Jan 13 '14 at 14:08
  • @ᴍarounᴍaroun How is `char[]` more specific for something that is the absence of an object? – Marko Topolnik Jan 13 '14 at 14:08
  • @MarkoTopolnik Apparently.. :) Well.. `null` is special.. I was inaccurate when I said it's the "absence" of an Object. – Maroun Jan 13 '14 at 14:09
  • @ᴍarounᴍaroun The answer to that is: the `null` type is sort-of the *bottom type* of Java. As such `char[]` is more specific than `Object`. And while we're discussing static type arithmetic, it is definitely acceptable to call `null` a kind of `Object`, being a value whose type is the subtype of `Object`. – Marko Topolnik Jan 13 '14 at 14:11
  • @MarkoTopolnik Always there for good explanations and comments, thanks. – Maroun Jan 13 '14 at 14:12

2 Answers2

3

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.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • 2
    A corollary to this is that if there is no single most specific overload (e.g. if there were also `valueOf(String)` - no one option is more specific than _all_ the other ones) then you'd get a compile error. – Ian Roberts Jan 13 '14 at 14:18
1

String.valueOf((Object) null) calls the following method:

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

As you can see, the null case is managed.

String.valueOf(null) calls the following method:

public static String valueOf(char data[]) {
    return new String(data);
}

Which itself calls:

public String(char value[]) {
    this.offset = 0;
    this.count = value.length; // <-- NPE
    this.value = StringValue.from(value);
}
sp00m
  • 47,968
  • 31
  • 142
  • 252