How to check if String is not null and empty ?

In Java, to avoid null pointer exception it is always advisable to have check for null. In Java for String null and empty String are two different things. For beginners this is may be good question to be asked in interview. So you need to have idea what empty String is and what String in null.

Let's first see when String is empty and when it is null.


private String firstName; // this has null value
 
private String lastName = ""; // this is empty String
 
private String middleName = "   "; // this is String with white spaces so its neither null nor empty.

There are different ways to check that if given String is not null and not empty. Let's see these different ways one by one.

1. Using .isEmpty() method of String.

String str = "John";
  
if (str != null && !str.isEmpty()) {
 System.out.println("str is not null and not empty");
}

So here first we have to check for null to avoid null pointer exception. Then after we are calling isEmpty() method of String and isEmpty() will return true if String is empty. So here String has value as "John" so this will return false and we have not (!) so it will become true and it will print out the statement that is inside if.

Remember in case of String with white space it will do the same because it will count white space as character. so isEmpty() will return false. If you want to consider String without white spaces then you need to call trim() method before calling isEmpty().


String str = "John";
  
if (str != null && !str.trim().isEmpty()) {
 System.out.println("str is not null and not empty");
}

2. Using length() method of String.
String str = "John";
  
if (str != null && str.length() > 0) {
 System.out.println("str is not null and not empty");
}
So here String str has 4 characters so length() method will return 4 hence its > 0. So this will print out the statement inside if.

Remember in case of String with white space it will do the same because it will count white space as character. If you want to consider String without white spaces then you need to call trim() method before calling length().


String str = "John";
  
if (str != null && str.trim().length() > 0) {
 System.out.println("str is not null and not empty");
}


3. Using Apache Commons Library. You first need to have this library in your class path then only you can use below code.
String str = "John";
  
if (!StringUtils.isEmpty()) {
 System.out.println("str is not null and not empty");
}

Here StringUtils.isEmpty() will return true if String is null OR String is empty.

So this is how you can check if String is not null and not empty. Also you can check by removing white spaces from String using trim() method.

Checkout Below Useful Courses :-
1) Learn RabbitMQ Messaging and its practical implementation with Java, Spring Boot and Spring MVC

2) Learn Spring Social SignIn with Twitter, LinkedIn. PayPal Payment gateway Integration in Spring MVC application, Java Messaging Service (JMS) using Spring Boot and Spring MVC, Apache Velocity to send Email

3) Java Messaging Service(JMS) With Spring MVC, Spring Boot, Apache ActiveMQ

4) Java Interview Preparation || 100+ Quality Questions Covered



0 Comments