This week's book giveaway is in the General Computing forum.
We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Difference between NULL and empty string Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Difference between NULL and empty string" Watch "Difference between NULL and empty string" New topic
Author

Difference between NULL and empty string

Vijay jai Singh
Greenhorn

Joined: Jan 07, 2009
Posts: 26
Hi,

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

Thank you
Vijay
Ankit Garg
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9191
    
    2

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
leroy tsruya
Ranch Hand

Joined: Sep 24, 2009
Posts: 57
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
Saloon Keeper

Joined: Aug 03, 2008
Posts: 9191
    
    2

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

Joined: Sep 26, 2009
Posts: 188
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

Joined: Sep 24, 2009
Posts: 57
wow im such a dumb.. of course its a method....
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32833
    
    4
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

Joined: Oct 02, 2003
Posts: 10043
    
    6

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.


Never ascribe to malice that which can be adequately explained by stupidity.
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
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
Sheriff

Joined: Sep 28, 2004
Posts: 16815
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rahul P Kumar
Ranch Hand

Joined: Sep 26, 2009
Posts: 188
thanks for this insight.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Difference between NULL and empty string
 
Similar Threads
Null string & empty string
while loop
Initializing reference variable
Empty string & null??
difference between poll memory and non-poll memory?