Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Null Object

Thread view: 
Moon2 - 20 Mar 2007 21:41 GMT
Can anyone please tell me why the following doesnt create a runtime error.

Object s = null;
System.out.println(s);

The output is null. Why doesnt it call the Object's tostring method and
throw a null pointer exception. Any comments will be appreciated.

Moon2
Stefan Ram - 20 Mar 2007 21:49 GMT
"Moon2" <none@none.com> writes (with minor modifications):
>Why doesnt it call the object's tostring method and
>throws a null pointer exception?

 This is the result of a a design decisions.
Stefan Ram - 20 Mar 2007 21:58 GMT
>This is the result of a a design decisions.

 I meant »This is the result of a design decision.«.

 Usually, when null is not a valid value, one wants a receiver
 of the value to mark failure as early as possible, e.g., by
 throwing.

 But it makes sense for some receivers to accept null as a
 possible value. This depends on the context.

 In the case of »println«, it is often more helpful to show the
 null than to abort the action, e.g., when used for debugging.
Mike  Schilling - 20 Mar 2007 22:01 GMT
> Can anyone please tell me why the following doesnt create a runtime error.
>
[quoted text clipped - 3 lines]
> The output is null. Why doesnt it call the Object's tostring method and
> throw a null pointer exception. Any comments will be appreciated.

Because the engineers who defined the method deemed that to be less useful
behavior that what it actually does.  Note that this isn't a general
language issue, it's specific to OutputStream.println(Object ).  If you'd
asked about

   Object s = null;
   String msg = "The answer is " + s;

Now it's a language issue, though the answer remains pretty much the same.
Oliver Wong - 20 Mar 2007 22:03 GMT
> Can anyone please tell me why the following doesnt create a runtime
> error.
[quoted text clipped - 4 lines]
> The output is null. Why doesnt it call the Object's tostring method and
> throw a null pointer exception. Any comments will be appreciated.

   System.out.println() doesn't actually directly call the passed-in
object's toString() method. Instead, it calls String.valueOf(s). The
implementation of the String.valoeOf(Object) method is:

public static String valueOf(Object obj) {
 return (obj == null) ? "null" : obj.toString();
}

   That is why you see the output "null" instead of an NPE.

   - Oliver
Moon2 - 20 Mar 2007 22:55 GMT
Thanks very much for clearing things up

Moon2
Hendrik Maryns - 21 Mar 2007 10:56 GMT
Moon2 schreef:
> Thanks very much for clearing things up

By the way, note that your subject is misleading: this is not about a
Null Object (there is non in the standard Java API), but about the null
*reference*.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.