Java check if string is null or empty – Java program to check the string is null or empty

Java check if string is null or empty: Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Program to check the string is null or empty

Concept :

An empty string is a string which is having of zero length, whereas a null string has no value at all.(i.e., no length) . while empty string is represented by ¬†“” . And a ¬†null string is represented by null .

Different Examples:

Solving approach:

  • Enter a string .
  • Check the sting type.
  • Print according to it.

Example-1: Checking null string

Checking a null string.

Program:

class Main 
{
  public static void main(String[] args)
  {
      //declare a string.
    String str = null ;
    // checking string
    if (str == null)
     System.out.println("String is "+"NULL");
    else if(str.isEmpty())
      System.out.println("String is "+ "empty");
    else 
      System.out.println("String containing somthing !!!");
  }
}
Output:

String is NULL

Example-2 : Checking a string static input

Checking a string static input.

Program:

import java.util.Scanner;
public class Main 
{  
    public static void main(String[] args)
    { 
    // String containg something i.e 'hello'
    String s1= "BtechGeeks"; 
    if(s1 .isEmpty())
      System.out.println("String is "+ "empty");
    else 
      System.out.println("String containing somthing !!!");
      
      // String containg blank or space
      String s2= "  "; 
      if(s2 .isEmpty())
      System.out.println("String is "+ "empty");
    else 
      System.out.println("String containing somthing !!!");
      
      //String containg nothing
      String s3= ""; 
      if(s3 .isEmpty())
      System.out.println("String is "+ "empty");
    else 
      System.out.println("String containing somthing !!!");
  }
}

Output:

String containing somthing !!!
String containing somthing !!!
String is Empty

Example-3 : Checking an empty string by user input

Checking an empty string by user input.

Program:

import java.util.Scanner;
public class Main {  
    public static void main(String[] args)
    {  
         // CREATING OBJECT 
        Scanner sc = new Scanner(System.in);  
        // TAKING STRING FORM USER 
        System.out.print("Enter string: ");  
        String s= sc.nextLine();   
        // checking string 
    if (s == null)
     System.out.println("String is "+"NULL");
    else if(s .isEmpty())
      System.out.println("String is "+ "empty");
    else 
      System.out.println("String containing somthing !!!");
  }
}
Output:

CASE-1
Enter string:
String is Empty

CASE-2
Enter string: BTechGeeks
String containing something !!!

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Core Java Programs: