Disassembling the class file shows this:
String.valueOf(null)
-> INVOKESTATIC java/lang/String.valueOf:([C)Ljava/lang/String;
String.valueOf((Object)null)
-> INVOKESTATIC
java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String;
In the case of an ambiguous method call, the compiler chooses the most
specific one. In this case, char[] is a subclass of Object, so char[] is
chosen.
But if there is no "most specific" one then the compiler gives an ambiguous
method call error.
For example, compile these two classes to see the difference:
Works:
public class Null {
Null() { foobar(null); } // char[] variant most specific
static void foobar(char[] x) { }
static void foobar(Object x) { }
}
Fails:
public class Null {
Null() { foobar(null); } // compiler error
static void foobar(char[] x) { }
static void foobar(String x) { }
}
Pretty tricky...
-Archie
On Mon, Feb 2, 2009 at 12:31 AM, <[email protected]> wrote:
> Very curious ...
>
> Source of String.valueOf(Object) from JDK 1.4.2_15
>
> /**
> * Returns the string representation of the <code>Object</code>
> argument.
> *
> * @param obj an <code>Object</code>.
> * @return if the argument is <code>null</code>, then a string equal to
> * <code>"null"</code>; otherwise, the value of
> * <code>obj.toString()</code> is returned.
> * @see java.lang.Object#toString()
> */
> public static String valueOf(Object obj) {
> return (obj == null) ? "null" : obj.toString();
> }
>
>
> The same for JDK 1.6.0_10
>
> public static String valueOf(Object obj) {
> return (obj == null) ? "null" : obj.toString();
> }
>
>
> But indeed:
> Java 1.3.0-C : NPE
> Java 1.4.2_15-b02: NPE
> Java 1.5.0_12-b04: NPE
> Java 1.6.0_02-b05: NPE
>
> If I add a cast then it works
> System.out.println(String.valueOf( (Object)null ));
>
> So the compiler must bind that to another method than
> String.valueOf(Object) ...
>
>
> But of course, simply returning "null" is much easier ;)
>
>
>
> Jan
>
>
>
> > -----Ursprüngliche Nachricht-----
> > Von: Martijn Kruithof [mailto:[email protected]]
> > Gesendet: Samstag, 31. Januar 2009 22:14
> > An: Ant Developers List
> > Betreff: Re: svn commit: r739577 -
> > /ant/core/trunk/src/main/org/apache/tools/ant/property/NullReturn.java
> >
> > Doubted it, checked it and it returned NullPointerException.
> >
> > D:\>type Test.java
> > public class Test
> > {
> > public static void main(String[] args)
> > {
> > System.out.println(String.valueOf(null));
> > }
> > }
> >
> > D:\>java Test
> > Exception in thread "main" java.lang.NullPointerException
> > at java.lang.String.<init>(Unknown Source)
> > at java.lang.String.valueOf(Unknown Source)
> > at Test.main(Test.java:6)
> >
> > br Martijn
> >
> > Matt Benson schreef:
> > > I always thought String.valueOf(null) returned "null".
> > > Was I on crack?
> > >
> > > -Matt
> > >
> > > P.S. Good to see you committing, Martijn...
> > >
> > > --- [email protected] wrote:
> > >
> > >
> > >> Author: jkf
> > >> Date: Sat Jan 31 17:10:50 2009
> > >> New Revision: 739577
> > >>
> > >> URL:
> > >> http://svn.apache.org/viewvc?rev=739577&view=rev
> > >> Log:
> > >> Original toString would always throw a NPE.
> > >>
> > >> Modified:
> > >>
> > >>
> > >>
> > >
> > ant/core/trunk/src/main/org/apache/tools/ant/property/NullReturn.java
> > >
> > >> Modified:
> > >>
> > >>
> > >
> > ant/core/trunk/src/main/org/apache/tools/ant/property/NullReturn.java
> > >
> > >> URL:
> > >>
> > >>
> > >
> > http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apach
> > e/tools/ant/property/NullReturn.java?rev=739577&r1=739576&r2=7
> 39577&view=diff
> > >
> > >
> > ==============================================================
> > ================
> > >
> > >> ---
> > >>
> > >>
> > >
> > ant/core/trunk/src/main/org/apache/tools/ant/property/NullReturn.java
> > >
> > >> (original)
> > >> +++
> > >>
> > >>
> > >
> > ant/core/trunk/src/main/org/apache/tools/ant/property/NullReturn.java
> > >
> > >> Sat Jan 31 17:10:50 2009
> > >> @@ -33,6 +33,6 @@
> > >> * {...@inheritdoc}
> > >> */
> > >> public String toString() {
> > >> - return String.valueOf(null);
> > >> + return "null";
> > >> }
> > >> }
> > >>
> > >>
> > >>
> > >>
> > >
> > >
> > >
> > >
> > >
> > >
> > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [email protected]
> > > For additional commands, e-mail: [email protected]
> > >
> > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [email protected]
> > For additional commands, e-mail: [email protected]
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>
>
--
Archie L. Cobbs