java

How to check if a string is null in Java

In this tutorial, we are going to see how to check if a string is null in Java using the == operator.
 

How to check if a string is null in Java
public class Main {
   public static void main(String[] args) {
      String str1 = "Welcome To StackHowTo!";
      String str2 = "";

      if(str1 == null || str1.length() == 0){
            System.out.println("The string str1 is null");
      }
      if(str2 == null || str2.length() == 0){
            System.out.println("The string str2 is null");
      }
   }
}

Output:

The string str2 is null
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *