Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #32760

Re: A small quiz

From Mike Amling <mamling@chaff.us>
Newsgroups comp.lang.java.programmer
Subject Re: A small quiz
Date 2015-10-02 09:18 -0500
Organization A noiseless patient Spider
Message-ID <mum3fg$h7b$1@dont-email.me> (permalink)
References (2 earlier) <c4e3bc51-840d-443c-ba12-b91b0e139d38@googlegroups.com> <mujk3i$3j8$1@dont-email.me> <45bae432-2df5-4878-832b-11c465d666d8@googlegroups.com> <mujoqo$mnr$1@dont-email.me> <a93a2c6f-706a-4d08-8c1c-d54de4d33443@googlegroups.com>

Show all headers | View raw


On 10/1/15 12:19 PM, Eric Douglas wrote:
> On Thursday, October 1, 2015 at 1:02:17 PM UTC-4, Eric Sosman wrote:
>>       A subtler quiz might be: Why does the final line crash when
>> the preceding line doesn't?  (And a further debate: Is the crash
>> a bug, or a feature, or a misfeature?)
>
> Error is
> Exception in thread "main" java.lang.NullPointerException
> 	at java.lang.String.<init>(Unknown Source)
> 	at java.lang.String.valueOf(Unknown Source)
>
> Why does passing in a null constant generate this runtime error, and not a compile error?
> It's just a confusing use of overload.
> http://stackoverflow.com/questions/3131865/why-does-string-valueofnull-throw-a-nullpointerexception

To preclude an unexpected choice of "Most Specific Method", I recommend 
always casting a null literal when used as a method argument.

System.out.println(String.valueOf((String)null)); // Prints "null".
System.out.println(String.valueOf((Object)null)); // Prints "null".
System.out.println(String.valueOf(null)); // NullPointerException

Mike Amling
RXZlbiB3aGVuIHRoZSBtZXRob2QgaXNuJ3Qgb3ZlcmxvYWRlZC4=

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Re: A small quiz Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-01 08:24 -0400
  Re: A small quiz e.d.programmer@gmail.com - 2015-10-01 08:33 -0700
    Re: A small quiz Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-01 11:41 -0400
      Re: A small quiz Eric Douglas <e.d.programmer@gmail.com> - 2015-10-01 08:59 -0700
        Re: A small quiz Jeff Higgins <jeff@invalid.invalid> - 2015-10-01 12:49 -0400
        Re: A small quiz Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-10-01 13:02 -0400
          Re: A small quiz Eric Douglas <e.d.programmer@gmail.com> - 2015-10-01 10:19 -0700
            Thanks (Was: A small quiz) j4n bur53 <janburse@fastmail.fm> - 2015-10-02 00:12 +0200
            Re: A small quiz Mike Amling <mamling@chaff.us> - 2015-10-02 09:18 -0500

csiph-web