Wednesday 19 August 2015

Java program to check if String is Null or Empty



                                    Java program to check if String is Null or Empty


While programming many times we come across the scenario where we have to validate the String object i.e. we need to check whether String is null or empty.

Here is the simple implementation to check whether String is null or empty.

 /**  
  * @author Dixit  
  *  
  */  
 public class EmptyOrNullStringCheck {  
      public static void main(String a[])  
      {  
           String firstString=null;  
           String secondString="abc";  
           String thirdString="";  
           System.out.println("Result of firstString:-"+isNullOrEmpty(firstString));  
           System.out.println("Result of secondString:-"+isNullOrEmpty(secondString));  
           System.out.println("Result of thirdString:-"+isNullOrEmpty(thirdString));  
      }  
      /**  
        * Checks for null or empty.  
        * @author dixit  
        * @param data true/ false  
        */  
        public static boolean isNullOrEmpty(String data) {  
         boolean retStatus = true;  
         if (data != null && !"".equals(data.trim())) retStatus = false;  
         return retStatus;  
        }  
 }  



Enjoy programming:)

No comments:

Post a Comment