Difference between null and empty ("") Java String

difference between null and empty list in java
difference between null and empty in php
difference between null and empty in oracle
difference between null and undefined
difference between null and empty set
difference between empty string and null string in automata
difference between null and empty string in c#
difference between null and empty in apex

What is the difference between null and the "" (empty string)?

I have written some simple code:

String a = "";
String b = null;

System.out.println(a == b); // false
System.out.println(a.equals(b)); // false

Both statements return false. It seems, I am not able to find what is the actual difference between them.

"" is an actual string, albeit an empty one.

null, however, means that the String variable points to nothing.

a==b returns false because "" and null do not occupy the same space in memory--in other words, their variables don't point to the same objects.

a.equals(b) returns false because "" does not equal null, obviously.

The difference is though that since "" is an actual string, you can still invoke methods or functions on it like

a.length()

a.substring(0, 1)

and so on.

If the String equals null, like b, Java would throw a NullPointerException if you tried invoking, say:

b.length()


If the difference you are wondering about is == versus equals, it's this:

== compares references, like if I went

String a = new String("");
String b = new String("");
System.out.println(a==b);

That would output false because I allocated two different objects, and a and b point to different objects.

However, a.equals(b) in this case would return true, because equals for Strings will return true if and only if the argument String is not null and represents the same sequence of characters.

Be warned, though, that Java does have a special case for Strings.

String a = "abc";
String b = "abc";
System.out.println(a==b);

You would think that the output would be false, since it should allocate two different Strings. Actually, Java will intern literal Strings (ones that are initialized like a and b in our example). So be careful, because that can give some false positives on how == works.

Difference between null and empty string, String s1 = ""; means that the empty String is assigned to s1 . In this case, s1.​length() is the same as "".length() , which will yield 0 as expected. String.Empty because it is a static variable, rather than "" which has to create a new string, and null means that you must then set the string equal to a new instance of a string. (thanks for the correction)

You may also understand the difference between null and an empty string this way:

Original image by R. Sato (@raysato)

Difference between null and empty ("") Java String, "" is an actual string, albeit an empty one. null, however, means that the String variable points to nothing. a==b returns false because "" and null  An empty string has a single element, the null character, '\0'. That’s still a character, and the string has a length of zero, but it’s not the same as a null string, which has no characters at all.

String is an Object and can be null

null means that the String Object was not instantiated

"" is an actual value of the instantiated Object String like "aaa"

Here is a link that might clarify that point http://download.oracle.com/javase/tutorial/java/concepts/object.html

What is the difference between null and empty?, So there are already a lot of answers which talk about these things in a technical sense… . But, If I am to conceptualize and understand these things with respect  The differences between Nothing, Empty, Missing, Null, Zero and ZLS 22 votes: 15,416 views No comments by Allen Browne, 18 May 2009 (for Access 97+)

What your statements are telling you is just that "" isn't the same as null - which is true. "" is an empty string; null means that no value has been assigned.

It might be more enlightening to try:

System.out.println(a.length()); // 0
System.out.println(b.length()); // error; b is not an object

"" is still a string, meaning you can call its methods and get meaningful information. null is an empty variable - there's literally nothing there.

The Difference Between Null, Empty And Zero-Length Data / Strings , The short version is this: If a variable simply doesn't exist — usually because it hasn't been declared, but sometimes because it hasn't been  Null means nothing. Its just a literal. Null is the value of reference variable. But empty string is blank.It gives the length=0. Empty string is a blank value,means the string does not have any thing.

There is a pretty significant difference between the two. The empty string "" is "the string that has no characters in it." It's an actual string that has a well-defined length. All of the standard string operations are well-defined on the empty string - you can convert it to lower case, look up the index of some character in it, etc. The null string null is "no string at all." It doesn't have a length because it's not a string at all. Trying to apply any standard string operation to the null string will cause a NullPointerException at runtime.

When to use NULL and when to use an empty string?, I don't believe the physical storage difference is worth worrying about in When you can distinguish between '' and NULL you are able to  "" is an empty string; null means that no value has been assigned. It might be more enlightening to try: System.out.println(a.length()); // 0 System.out.println(b.length()); // error; b is not an object "" is still a string, meaning you can call its methods and get meaningful information. null is an empty variable - there's literally nothing there.

21.2 Validating Null and Empty Strings, The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no  Also NULL is a absence of value, where a field having NULL is not allocated any memory, where as empty fields have empty value with allocated space in memory.

Solved: Blank vs. Null, Solved: What is the difference between a blank and a null record? Oracle:Difference between NULL and EMPTY string. Ask Question Asked 4 years, 1 month ago. Active 4 years, 1 month ago. Viewed 7k times 1. I am in a situation where my

What is the difference between empty and null in Salesforce , Null is nothing but the default value that is assigned to any variable, not initialized yet. At the same time, an empty string is blank and it will return the length as zero​  The value of Null may be missing, empty or could be anything. But blank only mean empty string. Like someone's middle name is empty just means he/she doesn't have a name, if middle name is Null, then it means the middle name is missing, could be empty, or "A" or "ABC" or anything.

Comments
  • Compare with b.equals(a) -- but don't use == for string comparing as "it won't work" in other ways. The null value (which is different than an empty string "", a valid String instance) can never have a method invoked upon it. Placing the "known non-null" (usually a constant value or literal) to the left side of the equality is "Yoda conditionals" or some-such.
  • Does this apply to C# also? As in ""'s array is {'\0'}, a null
  • The link about intern has expired. You can reference to another site to read about it: weblogs.java.net/blog/enicholas/archive/2006/06/…
  • So, if I have a null String String a = null and then I append it a String like a+= "example", when I print it, why does display nullexample if null is not a String?
  • @PinaGamer JavaScript allows adding non-strings to strings. "num: " + 20 gives you the string "num: 20". Does this mean that 20 is a string? (it's not, 20 is a number). Same case for null: it's not a string, but it can be converted to one if you try to add it.
  • @Zach L : when String s = null+"a"; it gives output nulla but null.concat("a") it gives the null pointer exception. what is the reason in my first case null+"a"; is working.
  • This is great! I was explaining null vs empty to somebody and happened upon your answer. The concept immediately clicked for them.
  • "null means that the String Object was not instantiated" - thank you! that helps me to understand things a lot. i was able to use an if statement on a MediaPlayer object once, and it worked to use null, to check if it was running or not (with a method to execute if it was), but i never understood why it worked, but now i see what it was saying, i was checking for whether MediaPlayer had been instantiated or not, by using null... e.g if (mp==null){do something}.
  • Both a and b are references. a is a reference with an instantiated object. b is a reference without an instantiated object (hence the term "null reference").
  • I counter-acted the -1 ;-) But it would help to clarify this answer and discuss the difference between "an object" and the null value and the difference between objects and variables.
  • @pst thanks :) I answered it by heart because here is my another answer that similar to this question stackoverflow.com/questions/4459623/…
  • The question is for Java