DEV Community

Cover image for java check if the string is empty.
jacob777baltimore
jacob777baltimore

Posted on

1

java check if the string is empty.

Java program to check if a string is empty or null:

In the above program, we check that the isEmpty() method will only check whether the String is empty or not. If the user wants to check whether the given String is null or empty then you can do this in following ways;

public class Example{

   public static void main(String args[]){ 

               String str1 = null;

               String str2 = "beginners book"; 

               if(str1 == null || str1.isEmpty()){

                  System.out.println("String str1 is empty or null");

               }

               else{

                  System.out.println(str1);

               }

               if(str2 == null || str2.isEmpty()){

                  System.out.println("String str2 is empty or null"); 

               }

               else{

                  System.out.println(str2);

               }

   }

}
Enter fullscreen mode Exit fullscreen mode

Read more

Output

String str1 is empty or null

beginners book
Enter fullscreen mode Exit fullscreen mode

Image of Stellar post

How a Hackathon Win Led to My Startup Getting Funded

In this episode, you'll see:

  • The hackathon wins that sparked the journey.
  • The moment José and Joseph decided to go all-in.
  • Building a working prototype on Stellar.
  • Using the PassKeys feature of Soroban.
  • Getting funded via the Stellar Community Fund.

Watch the video

Top comments (1)

Collapse
 
nyorja profile image
Rod Fetalvero

IMHO I will use StringUtils by Apache Commons

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more