How to check if a String has no characters, is empty in Java

1 Answer

0 votes
package javaapplication1;
 
public class JavaApplication1 {
 
    public static void main(String[] args) {
 
        try {
 
            String s = "";
            if (s.isEmpty()) {
                System.out.println("String is empty");
            }
            if (s.length() == 0) {
                System.out.println("String is empty");
            }
             
            if ("".equals(s)) {
                System.out.println("String is empty");
            }
             
            String empty = new String("");
            if (s.equals(empty)) {
                System.out.println("String is empty");
            }
 
        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}
 
/*
             
run:
       
String is empty
String is empty
String is empty
String is empty
    
 */

 



answered Nov 23, 2016 by avibootz
edited Nov 23, 2016 by avibootz

Related questions

1 answer 143 views
1 answer 165 views
1 answer 171 views
2 answers 269 views
1 answer 136 views
1 answer 147 views
...