• Post Reply Bookmark Topic Watch Topic
  • New Topic

String s = null behaviour  RSS feed

 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi All,

If I use the code

String s = null;
System.out.println(s.isEmpty());

as expected, It gives me null pointer exception.

But when I use the code
String s = null;
s = s + "hlo";
System.out.println(s);

It prints 'nullhlo'

Could anybody please explain this behaviour.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you do String concatenation like that, the values that are being concatenated are turned into Strings based on the various String#valueOf() methods. When you pass it a null value String.valueOf(Object) is called, and if you read that method it tells you that if the reference passed in to the method is null, then the String "null" is returned.
 
Ashwini Kashyap
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nipun,

I think you must first go through NullPointerException details once here.

When you try to access null object as you are doing using "s.isEmpty()" it is bound to throw this exception.

But in second scenario, you are appending it to other string and hence that output is getting displayed without exception.
I hope this is clear to you. Let me know if not.

Thanks,
Ashwini Kashyap
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you should find details of how the + operator handles nulls in the Java Language Specification. Follow the §5.1.11 link there.
 
Nipun Bahr
Ranch Hand
Posts: 55
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

thanks Ashwini, Steve and Campbell for your valuable inputs
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You should find details of how the + operator handles nulls in the Java Language Specification. Follow the §5.1.11 link there.

Wow, the "12 fiddlers" example undressed me.

Not that I'm hard to undress.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Witten wrote: . . . Not that I'm hard to undress.
This is a family website. That sort of thing is between you and your wife only

Have you not had problems like fiddlers 12 yourself? I presume it is a reference to the rhyme Old King Cole.
 
Paul Witten
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Paul Witten wrote: . . . Not that I'm hard to undress.
This is a family website. That sort of thing is between you and your wife only

Have you not had problems like fiddlers 12 yourself? I presume it is a reference to the rhyme Old King Cole.

Haha, not Old King Cole, just some "dreary" concatenation issues with String and int in that spec example (that they named The Fiddlers).

Surprised me. I never had the occasion to do a concatenation like that. It's that trick stuff that can gotcha at times. But heck, what's a debugger for? [Not for students apparently. What a shame.]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!