How to Check if a String is Empty or Null in Java Program
Java Methods
- In Java, a method is a block of code that performs a specific task and can be called from other parts of the program. Methods help to organize code and reduce duplication.
Methods can take input parameters and can return a value. - You must be familiar with following topics to understand the correspond example. Java if…else Statement, Java Methods, Java String isEmpty(),Java String trim().
To know more about Java Program to check if a string is empty or null in Java, Read the complete article.
Steps to check if a string is empty or null in Java
- Here are the steps to check if a string is empty or null in Java:, you can follow these steps:
Check for null: Use the if statement and the equality operator == to check if the string variable is null.
Check for empty: Use the if statement and the .length() method to check if the string length is zero.
Check for both: You can combine both checks using the && operator to make sure the string is not null and not empty.
Java String trim() :
The trim() method in Java is a method of the java.lang.String class. It returns a new string with leading and trailing white spaces removed. A white space is defined as a space, tab, line feed, carriage return, or other characters that represent a blank space.
Java String isEmpty() :
The isEmpty() method in Java is a method of the java.lang.String class. It returns a boolean value of true if the string is empty, and false otherwise. A string is considered empty if its length is 0.
Java if...else Statement
The if...else statement in Java is a control flow statement that allows you to execute a certain block of code based on a condition.
The condition can be any expression that returns a boolean value, such as a comparison operator, a logical operator, or a call to a method that returns a boolean value.
Let’s look at a Java Program to check if a string is empty or null in Java to perform certain operations.
Example 1: Java Program to check if a string is empty or null in Java
Run
public class Main { public static void main(String[] args) { String str = ""; // change the value of str to test different cases // check if str is null if (str == null) { System.out.println("str is null"); } // check if str is empty else if (str.length() == 0) { System.out.println("str is empty"); } // str is not null and not empty else { System.out.println("str is not empty or null"); } } }
Output
str is empty
Explanation:
Here's a step-by-step explanation of the code:
The code starts with the public class Main declaration, which defines the main class of the program.
In the main method, a string variable str is declared and initialized with an empty string value.
The program then uses an if statement to check if str is null:
If str is null, the program prints the message "str is null".
If str is not null, the program continues to the next else if statement, which checks if the length of str is zero:
If the length of str is zero, the program prints the message "str is empty".
If str is not null and its length is not zero, the program continues to the next else statement, which prints the message "str is not empty or null".
Example 2 : Java Program to check if a string is empty or null in Java
Run
public class Main { public static void main(String[] args) { String str = ""; // change the value of str to test different cases if (str == null || str.isEmpty()) { System.out.println("str is empty or null"); } else { System.out.println("str is not empty or null"); } } }
Output
An error occurred while reading the file: path/to/file (No such file or directory)
Explanation:
This code uses the || operator to check if str is either null or empty. If either condition is true, the program prints the message "str is empty or null".
If both conditions are false, the program prints the message "str is not empty or null".
Example 3: Java Program to Check if String with spaces is Empty or Null
Run
public class Main { public static void main(String[] args) { String str = " "; // string with spaces if (str == null || str.trim().isEmpty()) { System.out.println("String is empty or null"); } else { System.out.println("String is not empty or null"); } } }
Output
String is empty or null
Explanation:
The if statement checks whether the string str is null or empty after being trimmed.
The null check is done first, since it is possible for str.trim() to throw a NullPointerException if str is null.
The trim() method removes any leading and trailing spaces from the string.
The isEmpty() method returns true if the string is empty (contains no characters), and false otherwise.
If the string is null or empty after being trimmed, the program outputs the message "String is empty or null" to the console.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment