How to check whether a string is empty or not in Java

1 Answer

0 votes
package javaapplication1;

public class JavaApplication1 {
    
    public static void main(String[] args) {
  
        String s1 = "java php c++ PHP";
        String s2 = "";
        
        System.out.println(s1.isEmpty());
        System.out.println(s2.isEmpty());
                
        System.out.println(s1.length() == 0);
        System.out.println(s2.length() == 0);

        String s3 = null;
        System.out.println(s3.isEmpty());
    }    
}
   
/*
   
run:
   
false
true
false
true
Exception in thread "main" java.lang.NullPointerException
   
*/

 



answered Oct 26, 2016 by avibootz

Related questions

1 answer 172 views
1 answer 206 views
1 answer 208 views
2 answers 214 views
4 answers 347 views
...