Null Object

Discussion in 'Java' started by Moon2, Mar 20, 2007.

  1. Moon2

    Moon2 Guest

    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
    Moon2, Mar 20, 2007
    #1
    1. Advertising

  2. Moon2

    Stefan Ram Guest

    "Moon2" <> 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, Mar 20, 2007
    #2
    1. Advertising

  3. Moon2

    Stefan Ram Guest

    -berlin.de (Stefan Ram) writes:
    >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.
    Stefan Ram, Mar 20, 2007
    #3
  4. "Moon2" <> wrote in message
    news:etpgtd$rjt$...
    > 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.


    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.
    Mike Schilling, Mar 20, 2007
    #4
  5. Moon2

    Oliver Wong Guest

    "Moon2" <> wrote in message
    news:etpgtd$rjt$...
    > 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.


    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
    Oliver Wong, Mar 20, 2007
    #5
  6. Moon2

    Moon2 Guest

    Thanks very much for clearing things up

    Moon2
    Moon2, Mar 20, 2007
    #6
  7. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    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
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.5 (GNU/Linux)

    iD8DBQFGAQFge+7xMGD3itQRAjltAJwJ5ADS9SkcSySBObKW2xXGJ7QzIQCfdDBY
    xXRZK9uLUoMHI05HWIA5pKc=
    =HspG
    -----END PGP SIGNATURE-----
    Hendrik Maryns, Mar 21, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    16
    Views:
    7,293
    Mike Schilling
    Oct 12, 2005
  2. Replies:
    5
    Views:
    26,517
    Mike Schilling
    Mar 29, 2006
  3. Cirene
    Replies:
    1
    Views:
    799
    Alexey Smirnov
    Jun 9, 2008
  4. putty
    Replies:
    1
    Views:
    247
    putty
    Apr 5, 2005
  5. Tim Platt

    XMLHTTP - null is null or not an object

    Tim Platt, Jun 4, 2007, in forum: Javascript
    Replies:
    0
    Views:
    171
    Tim Platt
    Jun 4, 2007
Loading...

Share This Page