• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • 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: 321
24
Eclipse IDE Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry!

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

Cheers!
Chris
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • 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
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!