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:j...@kruithof.xs4all.nl] 
> 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...
> >
> > --- j...@apache.org 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: dev-unsubscr...@ant.apache.org
> > For additional commands, e-mail: dev-h...@ant.apache.org
> >
> >   
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscr...@ant.apache.org
> For additional commands, e-mail: dev-h...@ant.apache.org
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@ant.apache.org
For additional commands, e-mail: dev-h...@ant.apache.org

Reply via email to