difference between null and empty java string

difference between null and empty java string  using -'java,string,null,equals,referenceequals'

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

I have written some simple code like this:

String a = "";
String b = null;

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

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

asked Oct 19, 2015 by r3tt
0 votes

10 Answers

0 votes

"" 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.substring(0, 1)

and so on.

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


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("");

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";

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.

answered Oct 19, 2015 by rajeshujade
0 votes

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

answered Oct 19, 2015 by sachin wagh
0 votes

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.

answered Oct 19, 2015 by yogeshplv
0 votes

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.

answered Oct 19, 2015 by abhimca2006
0 votes

null means the name isn't referencing any instantiated object. "" means an empty string.

Here a is referencing some object which happens to be an empty string. b isn't referencing any object as it's null.

answered Oct 19, 2015 by mannumits1
0 votes

here a is an Object but b(null) is not an Object it is a null reference

System.out.println(a instanceof Object); // true

System.out.println(b instanceof Object); // false

here is my similar answer

answered Oct 19, 2015 by shegokar.anjeet
0 votes

In Java a reference type assigned null has no value at all. A string assigned "" has a value: an empty string, which is to say a string with no characters in it. When a variable is assigned null it means there is no underlying object of any kind, string or otherwise.

answered Oct 19, 2015 by vijaygupta1980
0 votes

The empty string is distinct from a null reference in that in an object-oriented programming language a null reference to a string type doesn't point to a string object and will cause an error were one to try to perform any operation on it. The empty string is still a string upon which string operations may be attempted.

From the wikipedia article on empty string.

answered Oct 19, 2015 by abhi
0 votes

"" and null both are different . the first one means as part of string variable declaration the string constant has been created in the string pool and some memory has been assigned for the same.

But when we are declaring it with null then it has just been instantiated jvm , but no memory has been allocated for it. therefore if you are trying to access this object by checking it with "" - blank variable , it can't prevent nullpointerexception . Please find below one use-case.

public class StringCheck {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    String s1 = "siddhartha";
    String s2 = "";
    String s3 = null;

    System.out.println("length s1 ="+s1.length());
    System.out.println("length s2 ="+s2.length());

    //this piece of code will still throw nullpointerexception . 
    if(s3 != ""){
        System.out.println("length s3 ="+s3.length());


answered Oct 19, 2015 by akhilesh
0 votes
String s = "";

String s = null;

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.

answered Oct 19, 2015 by okesh.badhiye