The default value of String in Java is one of the most common sources of confusion for beginners, especially when they first encounter null, empty strings, and uninitialized variables. In Java, String is not a primitive type; it is a reference type. Because of this, its default value behaves differently from primitive types such as int, double, or boolean. Which means understanding this distinction is essential for writing safe, predictable, and professional Java code. This guide explains what the default value of a String really is, why it matters, how it differs from an empty string, and how to avoid common mistakes such as NullPointerException.
What Does “Default Value” Mean in Java?
In Java, a default value is the value automatically assigned to a variable when it is not explicitly initialized. The Java runtime assigns default values only to certain kinds of variables, mainly instance variables and static variables. Local variables inside methods do not receive default values and must be initialized before use.
For primitive types, Java assigns predictable defaults:
intdefaults to0doubledefaults to0.0booleandefaults tofalsechardefaults to'\u0000'- Reference types default to
null
Since String is a reference type, its default value is null.
Simply put, if you declare a String field in a class and do not assign it a value, Java will automatically set it to null.
public class User {
String name; // default value is null
}
If you print this field before assigning it a value, the output will be:
null
Default Value of String in Java: null
The most important point to remember is this:
The default value of a String in Java is null, not an empty string.
Many beginners assume that an uninitialized String starts as "", but that is incorrect. Now, an empty string "" is still a real String object. It exists in memory and has a length of zero. By contrast, null means that the reference does not point to any object at all.
Consider this example:
public class Demo {
String message;
public static void main(String[] args) {
Demo demo = new Demo();
System.out.println(demo.
Here, `message` is an instance variable of type `String`. Because it was not initialized, Java assigns it the default value `null`.
The same rule applies to static variables:
```java
public class Demo {
static String username;
public static void main(String[] args) {
System.out.println(username); // prints null
}
}
That said, local variables are different. If you declare a local String variable and do not initialize it, Java will not assign a default value. Instead, the code will fail to compile if you try to use it.
public class Demo {
public static void main(String[] args) {
String city;
System.out.println(city); // compilation error
}
}
To fix this, you must assign a value before using the variable:
String city = "";
System.out.println(city);
Why the Default String Value Is Not an Empty String
The difference between null and "" is one of the most important concepts in Java. They may look similar when printed, but they behave very differently.
A String variable with value null does not reference any object:
String text = null;
A String variable