This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between NULL and empty string

 
Vijay jai Singh
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is difference between null string (String abc = null) and empty string (String abc = "")

Thank you
Vijay
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try to call a method on both of them and you'll see the difference



Basically a reference to an empty string points to an object in the heap so you can call methods on it. But a reference pointing to null has no object to point in the heap and thus you'll get a NullPointerException...
 
leroy tsruya
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi vijai,

when you declare

you actually declare a new String Object, and it means the variable (in this case - s) has a String Object associated to it. you can call String methods on that object.
it is like typing:

if you type:


however, when you declare

null means that the variable (the reference pointer) is empty. it is not pointing to any Object at all.
in that case if you type:


hope it helps!
 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
leroy tsruya wrote:



This will not compile. The length (which you wrote as lentgh ) is not a property, its a method, so use s.length()...
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing above replies now you know that in null case there is no object created and currently variable is pointing to nothing, while in later case, there is an empty string object created and variable is pointing to that. Now you take println in both cases and see what it prints.
 
leroy tsruya
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
wow im such a dumb.. of course its a method....
 
Campbell Ritchie
Sheriff
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think nothing of it; we all make that sort of mistake.

And there is another spelling error; it should read null not NULL in the thread title. Remember Java is case-sensitive and you can get all sorts of difficult-to-find errors if you put a capitaL letter in the wrong place.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12097
30
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think of an empty string ("") as a box that is empty.

a null string means you don't even have a box!!!

In all cases, you can refer to "the box that holds my comic books". The box may not exist, the box may exist but be empty, or the box may be full.
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok you take print of string in case it is null, it still prints your box. What is that? What is null?
 
Henry Wong
author
Marshal
Pie
Posts: 20989
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul.p Kumar wrote:ok you take print of string in case it is null, it still prints your box. What is that? What is null?


No it doesn't "print your box". There is a special check in the print to see if it is null, and replaces it with "null", so it can be printed.

Or to stay with the metaphor... when you ask your friend to tell you what's in the box, he will tell you what is in the box, but if there is no box, will tell you that there is no box.

Henry
 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for this insight.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic