Java – Create an Empty String
To create an empty string in Java, you can use String class, or double quotes.
1. Create an empty string using String class in Java
To create an empty string using String class, create a new instance of String class, and pass no arguments to the constructor.
String myString = new String();
This statement initializes myString variable with an empty string.
Now, let us write a Java program, to create an empty string using String class.
Java Program
public class Main {
public static void main(String[] args) {
String myString = new String();
System.out.println("String value : \"" + myString + "\"");
}
}
Output
String value : ""
2. Create an empty string using double quotes in Java
To create an empty string using double quotes, specify the opening and closing double quotes with characters in between them.
String myString = "";
This statement initializes myString variable with an empty string.
Now, let us write a Java program, to create an empty string using double quotes.
Java Program
public class Main {
public static void main(String[] args) {
String myString = "";
System.out.println("String value : \"" + myString + "\"");
}
}
Output
String value : ""
Conclusion
In this Java String tutorial, we have seen how to create an empty string using String class or double quotes, with examples.