Check if a String is not empty ("") and not null in Java

Java 8Object Oriented ProgrammingProgramming

Let’s say we have the following string −

String myStr1 = "Jack Sparrow";

Let us check the string now whether it is not null or not empty.

if(myStr != null || myStr.length() != 0) {
System.out.println("String is not null or not empty");

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      String myStr = "Jack Sparrow";
      boolean res;
      if(myStr != null || myStr.length() != 0) {
         System.out.println("String is not null or not empty");
      } else {
         System.out.println("String is null or empty");
      }
   }
}

Output

String is not null or not empty
raja
Published on 06-Dec-2018 16:15:58
Advertisements