How to check if a string is null in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String str = null;
        
        if (str == null) 
            System.out.println("yes");
        else
            System.out.println("no");
            
        str = "";
        
        if (str == null) 
            System.out.println("yes");
        else
            System.out.println("no");            
    }
}
  
  
  
  
/*
run:
  
yes
no
 
*/

 



answered Jan 15, 2022 by avibootz

Related questions

1 answer 226 views
2 answers 108 views
1 answer 90 views
2 answers 217 views
2 answers 216 views
2 answers 176 views
176 views asked Nov 23, 2016 by avibootz
...