Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The issue is that <code>String.valueOf</code> method is <em>overloaded</em>:</p> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#valueOf%28java.lang.Object%29" rel="noreferrer"><code>String.valueOf(Object)</code></a></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html#valueOf%28char%5B%5D%29" rel="noreferrer"><code>String.valueOf(char[])</code></a></li> </ul> <p>Java Specification Language mandates that in these kind of cases, <em>the most specific overload</em> is chosen:</p> <h3><a href="http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.12.2.5" rel="noreferrer">JLS 15.12.2.5 Choosing the Most Specific Method</a></h3> <blockquote> <p>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 <em>most specific</em> method is chosen.</p> </blockquote> <p>A <code>char[]</code> <em>is-an</em> <code>Object</code>, but not all <code>Object</code> <em>is-a</em> <code>char[]</code>. Therefore, <code>char[]</code> is <em>more specific</em> than <code>Object</code>, and as specified by the Java language, the <code>String.valueOf(char[])</code> overload is chosen in this case.</p> <p><code>String.valueOf(char[])</code> expects the array to be non-<code>null</code>, and since <code>null</code> is given in this case, it then throws <code>NullPointerException</code>.</p> <p>The easy "fix" is to cast the <code>null</code> explicitly to <code>Object</code> as follows:</p> <pre><code>System.out.println(String.valueOf((Object) null)); // prints "null" </code></pre> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/2659496/how-does-polymorph-ambiguity-distinction-work/">How does polymorph ambiguity distinction work?</a></li> <li><a href="https://stackoverflow.com/questions/1545501/which-overload-will-get-selected-for-null-in-java">Which overload will get selected for null in Java?</a></li> </ul> <hr> <h3>Moral of the story</h3> <p>There are several important ones:</p> <ul> <li><em>Effective Java 2nd Edition, Item 41: Use overloading judiciously</em> <ul> <li>Just because you can overload, doesn't mean you should every time</li> <li>They can cause confusion (especially if the methods do wildly different things)</li> </ul></li> <li>Using good IDE, you can check which overload is selected at compile time <ul> <li>With Eclipse, you can mouse-hover on the above expression and see that <em>indeed</em>, the <code>valueOf(char[])</code> overload is selected!</li> </ul></li> <li>Sometimes you want to explicitly cast <code>null</code> (examples to follow)</li> </ul> <h3>See also</h3> <ul> <li><a href="https://stackoverflow.com/questions/154577/polymorphism-vs-overriding-vs-overloading">Polymorphism vs Overriding vs Overloading</a></li> <li><a href="https://stackoverflow.com/questions/248222/method-overloading-can-you-overuse-it">Method Overloading. Can you overuse it?</a></li> </ul> <hr> <h3>On casting <code>null</code></h3> <p>There are at least two situations where explicitly casting <code>null</code> to a specific reference type is necessary:</p> <ul> <li>To select overloading (as given in above example)</li> <li>To give <code>null</code> as a single argument to a vararg parameter</li> </ul> <p>A simple example of the latter is the following:</p> <pre><code>static void vararg(Object... os) { System.out.println(os.length); } </code></pre> <p>Then, we can have the following:</p> <pre><code>vararg(null, null, null); // prints "3" vararg(null, null); // prints "2" vararg(null); // throws NullPointerException! vararg((Object) null); // prints "1" </code></pre> <h3>See also</h3> <ul> <li><a href="http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html" rel="noreferrer">Java Language Guide/varargs</a> - to understand how it's implemented</li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/315846/why-null-cast">Why null cast?</a></li> <li><a href="https://stackoverflow.com/questions/2888305/difference-between-double-and-double-in-formal-parameter-type-declaration/">Difference between double… and double[] in formal parameter type declaration</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload