Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Why does "String.valueOf(null)" call the array overload?

Reply
Thread Tools

Why does "String.valueOf(null)" call the array overload?

 
 
Hosam Aly
Guest
Posts: n/a
 
      10-27-2007
Hello,

I was playing with some code today, and then I hard-coded the line:
System.out.println(String.valueOf(null));

At first, I expected this line to call the overload
"String.valueOf(Object)", printing "null" to the output stream.
However, I noticed that it called the "String.valueOf(char[])"
overload, throwing a NullPointerException.

Although my first expectation is incorrect, I am thinking about why
the char[] overload was called automatically, and why the compiler
didn't complain about this being an ambigious call, in the same way it
would do had there been another overload like
"String.valueOf(Integer)".

I guess this either has to do with the Java Language Specification, or
a decision made for the compiler. Could you please explain to me why
the compiler takes this behavior? In my opinion, it would have been
safer to complain about it. Am I wrong? Why?


Thanks a lot for your help.

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-27-2007
On Sat, 27 Oct 2007 01:45:32 -0700, Hosam Aly <(E-Mail Removed)>
wrote, quoted or indirectly quoted someone who said :

>At first, I expected this line to call the overload
>"String.valueOf(Object)", printing "null" to the output stream.
>However, I noticed that it called the "String.valueOf(char[])"
>overload, throwing a NullPointerException.


According to http://java.sys-con.com/read/34292_2.htm
that is a bug. It should complain of ambiguity.

There are Delphic Oracles who can divine meaning in the incantations
of the JLS. We must await their definitive answer.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      10-27-2007
Hosam Aly wrote:
> Hello,
>
> I was playing with some code today, and then I hard-coded the line:
> System.out.println(String.valueOf(null));
>
> At first, I expected this line to call the overload
> "String.valueOf(Object)", printing "null" to the output stream.
> However, I noticed that it called the "String.valueOf(char[])"
> overload, throwing a NullPointerException.
>
> Although my first expectation is incorrect, I am thinking about why
> the char[] overload was called automatically, and why the compiler
> didn't complain about this being an ambigious call, in the same way it
> would do had there been another overload like
> "String.valueOf(Integer)".
>
> I guess this either has to do with the Java Language Specification, or
> a decision made for the compiler. Could you please explain to me why
> the compiler takes this behavior? In my opinion, it would have been
> safer to complain about it. Am I wrong? Why?


See
http://java.sun.com/docs/books/jls/s...doc.html#18428

The accessible, applicable methods for the call are valueOf(Object) and
valueOf(char []). The other valueOf methods in String either require
more arguments, or have a primitive argument type.

char[] is a subclass of Object, so the char[] method is unambiguously
more specific than the valueOf(Object) method.

This is an artificial case, because the explicit "null" makes the type
of the expression the null type, which is can be converted by widening
reference conversion to any reference type. That convertibility is why
you can write:

char[] x = null;

with no explicit cast.

Normally, you would have a null expression of some other type, such as a
null String reference. In that case, the char[] version would not be
applicable unless the type of the expression were char[].

Patricia
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 03-09-2011 11:00 PM
FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? PerlFAQ Server Perl Misc 0 02-11-2011 05:00 AM
Why can't I call a function which does what it does if a click on theheader of a column? Stefan Mueller Javascript 23 12-14-2009 06:07 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM



Advertisments