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


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 25-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements