🌐
Coderanch
coderanch.com › t › 397787 › java › Declare-initialize-empty-String
Declare and initialize empty String (Beginning Java forum at Coderanch)
First, you need a variable name in there for it to be legal. Question: does line 2 initialize 'b' to an empty string? Line 1 create a String object and assigns it the reference 'a'. Line 2 only creates a reference( 'b' ) to a String object. It will need to be assigned a String object before it can be used.
🌐
Baeldung
baeldung.com › home › java › java string › string initialization in java
String Initialization in Java | Baeldung
January 8, 2024 - As we know by now, the emptyLiteral will be added to the String pool, while the other two go directly onto the heap.
🌐
Medium
medium.com › @rahul.tpointtech12 › exploring-the-possibilities-of-empty-string-in-java-f6f2b300ae70
Exploring the Possibilities of Empty String in Java | by Rahul | Medium
March 20, 2025 - An empty string in Java is a string literal with no characters between the double quotes: “”. It is different from null, which signifies the absence of a string object altogether.
🌐
iO Flood
ioflood.com › blog › empty-string-java
Working with Java Empty String: A Detailed Guide
February 20, 2024 - In this revised code, str.trim().isEmpty() returns true because trim() removes the space from str, making it an empty string. The Java String class is one of the most commonly used classes in Java. It represents a sequence of characters (or a string of text). An instance of the String class can be created in two ways:
🌐
Quora
quora.com › How-can-I-empty-a-String-in-Java
How to empty a String in Java - Quora
Answer (1 of 11): You can't. Strings are immutable. You can just reference a new string object that has 0 characters. So if you have the string [code] String whoIsAwesome = "you"; [/code] you can assign a new String object to your variable like ...
🌐
Reddit
reddit.com › r/programmerhumor › how to create an empty string
r/ProgrammerHumor on Reddit: How To Create an Empty String
October 20, 2019 - Though I suppose this could be Java where you use String and not string like C#. Would be a coincidence if you guys have a StringBuilder too. ... Was going to post the lovely string.Empty as well....
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › java › empty string in java
How to Check Empty String in Java | Delft Stack
February 2, 2024 - We are going to use the apache commons library and Java 11 String isBlank() method. Let’s start with some examples. Let’s create an empty string by assigning an empty value to it by using the empty double-quotes.
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-initialize-and-compare-strings-in-java
How to Initialize and Compare Strings in Java? - GeeksforGeeks
July 23, 2025 - Immutability creates two general ways of initialization with strings in Java using the String Pool.
🌐
TutorialKart
tutorialkart.com › java › java-create-an-empty-string
Java - Create an Empty String
August 19, 2023 - To create an empty string using String class, create a new instance of String class, and pass no arguments to the constructor. ... This statement initializes myString variable with an empty string.
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.

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.

🌐
Quora
quora.com › What-is-the-difference-between-initializing-a-string-to-null-and-initializing-a-string-to-an-empty-string-for-the-Java-language
What is the difference between initializing a string to null and initializing a string to an empty string (for the Java language)? - Quora
Answer (1 of 12): Declaring a String in the following way [code]String s = null; [/code]will throw a NullPointerException if you try to access it before initializing it, or giving it some value, which means, the String s has not yet been allocated space in memory.
🌐
Codemia
codemia.io › knowledge-hub › path › why_is_there_no_stringempty_in_java
Why is there no String.Empty in Java?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises