Tester si un string est null | Java

Dans ce tutoriel nous allons découvrir comment tester si un string est null en Java en utilisant l’opérateur ==.
 
 

Exemple:
public class Main {
   public static void main(String[] args) {
      String str1 = "Welcome To WayToLearnX";
      String str2 = "";

      if(str1 == null || str1.length() == 0){
            System.out.println("La chaine str1 est null");
      }
      if(str2 == null || str2.length() == 0){
            System.out.println("La chaine str2 est null");
      }
   }
}

 
Sortie:

La chaine str2 est null

 

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *