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

Original image by R. Sato (@raysato)

Answer from mikiqex on Stack Overflow
Top answer
1 of 16
408

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

Original image by R. Sato (@raysato)

2 of 16
251

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

๐ŸŒ
Oracle
docs.oracle.com โ€บ javaee โ€บ 7 โ€บ tutorial โ€บ bean-validation002.htm
21.2 Validating Null and Empty Strings - Java Platform, Enterprise Edition: The Java EE Tutorial (Release 7)
An empty string is represented as "". It is a character sequence of zero characters. A null string is represented by null. It can be described as the absence of a string instance. Managed bean elements represented as a JavaServer Faces text component such as inputText are initialized with the ...
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ difference between null and empty string in java
Difference Between null and Empty String in Java | Baeldung
April 19, 2024 - If we assign null to a String object, itโ€™s initialized but not instantiated and hence holds no value or reference. An empty String is a valid String object having no characters, and as a result, all the String operations are available on this ...
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ java-check-if-string-is-null-empty-or-blank
Java: Check if String is Null, Empty or Blank
February 28, 2023 - String string = "Hello there"; if (string == null || string.equals("") || string.trim().equals("")) System.out.println("String is null, empty or blank"); else System.out.println("String is neither null, empty nor blank"); In much the same fashion as the before, if the trimmed string is "", it was either empty from the get-go, or was a blank string with 0..n whitespaces: ... The Apache Commons is a popular Java library that provides further functionality.
๐ŸŒ
Quora
quora.com โ€บ What-is-difference-between-EMPTY-and-NULL-string-in-Java
What is difference between EMPTY and NULL string in Java? - Quora
Answer (1 of 12): The main difference between null and empty is that the null is used to refer to nothing while empty is used to refer to a unique string with zero length. * A String refers to a sequence of characters.
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ strings in java โ€บ java: check if string is null, empty or blank
Java: Check if String is Null, Empty or Blank
October 11, 2023 - The String = null The String = Lubaina Khan ยท โ€œAn empty String in Java means a String with length equal to zero.โ€ If a String is empty that means the reference variable is referring to a memory location holding a String of length equal to zero.
Find elsewhere
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ checking for empty or blank strings in java
Checking for Empty or Blank Strings in Java | Baeldung
January 8, 2024 - String#isEmpty was introduced with Java 6. For Java 5 and below, we can use String#length instead: boolean isEmptyString(String string) { return string == null || string.length() == 0; } In fact, String#isEmpty is just a shortcut to String#length. Both String#isEmpty and String#length can be used to check for empty strings.
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ java โ€บ difference between null and empty strings in java
Null and Empty String in Java | Delft Stack
October 12, 2023 - Null is like a vacuum with some property associated with it, due to which we can neither consider it to be empty nor full. In Java, a string refers to the sequence of characters.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 464778 โ€บ java โ€บ Difference-NULL-empty-string
Difference between NULL and empty string (Beginning Java forum at Coderanch)
hi vijai, when you declare you ... 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 ...
Top answer
1 of 3
11

First let's clarify something: You mention that after assigning null to the variable you could forget to initialize it, but by assigning null to it you are in effect initializing it.

public static void main (String args[]){
    String s;       
    System.out.println(s); // compiler error variable may not be initialized
}

vs

public static void main (String args[]){
    String s=null;      
    System.out.println(s); // no compiler error
    System.out.println(s.equals("helo")); // but this will generate an exception
}

So after you do String s=null; there's is no way that you could forget to initialize because you did initialize it.

That being clear, I would recommend you to use a "smart default". In your case perhaps the empty string "" would be a good default value if you want to avoid NullPointerException. In the other hand, sometimes it is desirable that the program produce an exception because it indicates something wrong happened under the hood that should not have happened.

2 of 3
8

In general you want to keep declaration and initialisation as close as possible to minimise exactly the type of problem you're talking about.

There is also the issue of redundant initialisation where the value null you're assigning is never used which is extra code that harms readability even if the redundant assignment is optimised away by the compiler.

Sometimes assigning some sort of default value is unavoidable, for example if you declare before a try catch, initialise inside and use it afterwards. For other types you can often find a more natural default value such as an empty list.

๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 10 โ€บ check-if-string-is-null-empty-or-blank-in-java
Check if String is Null, Empty or Blank in Java
Simply compare the string with null to check for null string. Use isEmpty() method of string class to check for empty string. The isEmpty() method returns true if the string does not contain any value. Use trim() and isEmpty() method together to check for blank string.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ examples โ€บ string-empty-null
Java Program to Check if a String is Empty or Null
This is because white spaces are treated as characters in Java and the string with white spaces is a regular string. Now, if we want the program to consider strings with white spaces as empty strings, we can use the trim() method. The method removes all the white spaces present in a string. class Main { public static void main(String[] args) { // create a string with white spaces String str = " "; // check if str1 is null or empty System.out.println("str is " + isNullEmpty(str)); } // method check if string is null or empty public static String isNullEmpty(String str) { // check if string is null if (str == null) { return "NULL"; } // check if string is empty else if (str.trim().isEmpty()){ return "EMPTY"; } else { return "neither NULL nor EMPTY"; } } }
๐ŸŒ
Medium
medium.com โ€บ @ecetasci.iu โ€บ checking-for-null-or-empty-strings-in-java-19518fa1e553
Checking for Null or Empty Strings in Java | by Ece Tasci | Medium
February 25, 2025 - To prevent this, it is crucial to check for null before performing operations on a variable. An empty String ("") has zero characters but still exists in memory, specifically in the heap where Java stores objects.
๐ŸŒ
Java67
java67.com โ€บ 2014 โ€บ 09 โ€บ right-way-to-check-if-string-is-empty.html
Right way to check if String is empty in Java with Example | Java67
Another popular and faster way to check if String is empty or not is by checking its length like if String.length() = 0 then String is empty, but this is also not null safe. A third common way of checking the emptiness of String in Java is comparing it with empty String literal like "".equals(str), this method is not as fast as the previous two but it is null safe, you don't need to check for null, in case of null it will return false.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjava โ€บ shorter (efficient) way to check if a string is null or empty?
Shorter (efficient) way to check if a string is null or empty? : r/learnjava
March 4, 2020 - The if statement says "if it's null OR it's empty" So, if the string is null the "isEmpty" part will NOT be executed.