aspose file tools*
The moose likes Java in General and the fly likes what is the difference between NULL and Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "what is the difference between NULL and " "" Watch "what is the difference between NULL and " "" New topic
Author

what is the difference between NULL and " "

Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 670
what is the difference between NULL and " ".Are the below assignments same?

String str1=NULL;
String str2=" ";


thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61761
    
  67

There no such thing as NULL; it's null.

null means no reference. "" is a string literal representing a String object.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10535
    
    9

To give an example

#1 There is no baby name because there is no baby
#2 There is no baby name because it is not named yet, the baby does exist though


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 670
thanks all.

Which is the proper way to initialize the string



or


Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 670
I have initialized strings both ways in the past.I did not realise both are different.Which is the proper way?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
Well, "String str1=NULL" it certainly ain't, because, as Bear said, there is no such thing as NULL in Java. The answer depends entirely on the context of your app. Maneesh has already pointed out how the two ways might be interpreted differently. Note that initializing a String to " " -a single space- is very unusual. The empty string -""- is much more common.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42913
    
  68
I did not realise both are different.

Here's a very important question, then: What do you think "null" means? I'm baffled how one could think that it's the same as a one-character string.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11497
    
  16

saying

String str1 = " ":

gives you a String object that has one character in it - that character is a space. You have a box, and there is something in the box.

saying

String str1 = "";

gives you a String object with no characters in it. It's like having a box with nothing in it.

saying

String str1 = null;

gives you no String object. You have a name for your box, but you don't have a box.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Monica. Shiralkar
Ranch Hand

Joined: Jul 07, 2012
Posts: 670
Sorry for spelling null as NULL. Its a typing mistake.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the difference between NULL and " "