Java tostring with null

Null and toString()

send pies

posted 11 years ago

  • Report post to moderator
  • Hi all,
    I want to ask about the following code snippet

    My question is ,in the first case,toString() is invoked implicitly on null,and it compiled and ran just fine,but then what is wrong with second line.

    Bartender

    Chrome

    send pies

    posted 11 years ago

  • Report post to moderator
  • toString() is only called implicitly if the object passed isn’t null. If it is, the literal «null» is printed instead.

    Bartender

    send pies

    posted 11 years ago

    • 1
  • Report post to moderator
  • Sudhanshu Mishra wrote: Hi all,
    I want to ask about the following code snippet

    My question is ,in the first case,toString() is invoked implicitly on null,

    No, it’s not. Somewhere down in one of the methods that println() calls, there’s code that’s something like

    but then what is wrong with second line.

    You’re dereferencing a null pointer.

    Источник

    Читайте также:  Ссылки
    Оцените статью