• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String.toString() or not String.toString()?

 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

Perhaps I'm over analyzing this. I've been going through the applicable classes in preparation for the OCA.
I'm trying to understand why this prints null:

And this throws a NullPointerException:

I understand that the String object is null, and therefore any call to a String method, such as length() or trim(), will throw a NullPointerException. My confusion is why System.out.println(nullString) doesn't throw a NPE if it is calling the String.toString() method? As I understand it, System.out is a PrintStream and any call to println(Object o) calls the toString() of the Object.

I figured maybe the String.toString() method was written as:

But, all it says is:


So, where does the "null" literal value come from?

Cheers!
Chris
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops! Never mind, I kept diving and figured it out.

There is a "null" literal, but it's in the PrintStream, which actually doesn't call println(Object o) for Strings, but has an overload println(String s), that then calls print(String s) (itself, being an overload of print(Object o)) which says:

So, the literal value of "null" is sort of hidden in the PrintStream and is only applicable for display when the passed parameter type is a String.
Which I guess sort of makes sense, since the String is still really null in the program, but only visually "null" when it's being printed.

Cheers!
Chris
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris R Barrett wrote:Oops! Never mind, I kept diving and figured it out.



Glad you figured it out on your own -- as it is a better learning experience when you do.

Also, glad you came back and informed us -- as there are many who will not do that. Have a cow !!

Henry
 
Chris Barrett
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry!

Yes, learn by doing is so much faster than learn by reading.

Cheers!
Chris
 
Marshal
Posts: 79263
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done again
The PrintStream documentation does say it prints "null" if null is passed.

There must be people getting all confused because they have Strings which really say n‑u‑l‑l
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chris R Barrett wrote:There is a "null" literal, but it's in the PrintStream, which actually doesn't call println(Object o) for Strings, but has an overload println(String s), that then calls print(String s) (itself, being an overload of print(Object o))...


Ah, but that doesn't explain why println() will print out "null" for ANY null object, not just Strings. For that, you need to look at the println(Object) docs.

Winston
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic