Check if a string is empty or null or not in Java

In this tutorial, we will learn how to check if a string is empty or null or not in Java.

We call string is empty when it has length zero or have value  “”   it means no character stored in this string. and one string has a null value when it is initialized by a null value.

for checking if a string value is null or empty many ways are there.

  1. check the length of string
    if the length of string was zero this string is empty and when we call length() method the result comes java.lang.NullPointerException means the value of the string is null.
    Example:

    public class Codespeedy {
    
        public static void main(String[] args) {
            String str1="";
            System.out.println(str1.length()); // lenght is 0
            String str2=null;
            System.out.println(str2.length());  // java.lang.NullPointerException 
        }
    }

    outPut:

    0  
    
    Exception in thread "main" java.lang.NullPointerException
  2. check with String.equals() method
    we can check to bypass “” in the string, if the method returns true it meant string is empty if the value of string was null java.lang.NullPointerException will be the result.
    Example:

    public class Codespeedy {
    
        public static void main(String[] args) {
            String str1="";
            System.out.println(str1.equals(""));   // it will return true
            String str2=null;
            System.out.println(str2.equals(""));  // Exception will occure
        }
    }

    outPut

    true
    Exception in thread "main" java.lang.NullPointerException
  3. check with String.isEmpty() method
    this function is used to check whether the string is empty or not it will return true if string was empty and return false if string was not empty.
    Example:

    public class Codespeedy {
    
        public static void main(String[] args) {
            String str1="";
            System.out.println("is empty :"+ str1.isEmpty());
            String str2=null;
            System.out.println("is null :"+str2.isEmpty());
        }
    }

    outPut:

    is empty :true
    Exception in thread "main" java.lang.NullPointerException

 

Is the default value of a String is null or empty or none of them in Java

the default value of a string is empty, not null.

program:

public class Codespeedy {

    public static void main(String[] args) {
        String str=new String();
        System.out.println("is empty by length method: "+ str.length());
        System.out.println("is empty? by equals method: "+ str.equals(""));
        System.out.println("is empty by empty method: "+ str.isEmpty());
        System.out.println("is null by equals method: "+str.equals(null));
    }
}

outPut:

is empty by length method: 0
is empty? by equals method: true
is empty by empty method: true
is null by equals method: false

Check if StringBuffer is empty

StringBuffer is also empty same to string but for checking, we can use all ways used for the string but first, we have to convert StringBuffer to a string by using the toString() method.

Example:

public class Codespeedy {

    public static void main(String[] args) {
        StringBuffer str=new StringBuffer();
        System.out.println("is empty by length method :"+ str.length());
        System.out.println("is empty? by equals method :"+ str.toString().equals(""));
        System.out.println("is empty by empty method "+str.toString().isEmpty());
        System.out.println("is null by equals method :"+str.toString().equals(null));
    }
}

outPut:

is empty by length method :0
is empty? by equals method :true
is empty by empty method true
is null by equals method :false

Also read:

Leave a Reply

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