• 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
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Difference between NULL and empty string

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

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

Thank you
Vijay
 
Sheriff
Posts: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9697
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()...
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wow im such a dumb.. of course its a method....
 
Marshal
Posts: 77157
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 23931
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for this insight.
 
reply
    Bookmark Topic Watch Topic
  • New Topic