Java >> Tutoriel Java >  >> Java

Comment vérifier qu'un objet est nul en Java ?

  • Avec l'aide de "==" est utile pour la comparaison de référence et il compare deux objets.
  • "==" renvoie vrai si les deux références (objets) pointent vers le même emplacement mémoire, sinon il renverra faux si les deux objets pointent vers un emplacement mémoire différent.
  • nul est un mot-clé introduit en java qui est utilisé pour vérifier si un objet est nul ou non.
  • La signification de null sous une forme différente est "aucun objet" ou "inconnu" .
  • Nous verrons un programme pour vérifier si un objet est nul ou non.

Exemple :

public class ToCheckNullObject {
    public static void main(String[] args) {

        // We created a string object with null
        String str1 = null;

        // By using == operator to compare two objects 
        // and with the help of null we will be easily identify 
        // whether object is null or not
        if (str1 == null) {
            System.out.println("Given object str1 is null");
            System.out.println("The value of the object str1 is " + str1);
        } else {
            System.out.println("Given object  str1 is not null");
            System.out.println("The value of the object str1 is " + str1);
        }

        // We created a string object with specified value
        String str2 = "Welcome in Java World";

        // By using == operator to compare two objects 
        // and with the help of null we will be easily identify 
        // whether object is null or not
        if (str2 == null) {
            System.out.println("Given object str2 is null");
            System.out.println("The value of the object str2 is " + str2);
        } else {
            System.out.println("Given object str2 is not null");
            System.out.println("The value of the object str2 is " + str2);
        }

        // We created a string object with specified value
        String str3 = " ";

        // By using == operator to compare two objects and 
        // with the help of null we will be easily identify 
        // whether object is null or not
        if (str3 == null) {
            System.out.println("Given object str3 is null");
            System.out.println("The value of the object str3 is " + str3);
        } else {
            System.out.println("Given object str3 is not null");
            System.out.println("The value of the object str3 is " + str3);
        }

        // We created an integer object with null
        Integer i1 = null;

        // By using == operator to compare two objects and 
        // with the help of null we will be easily identify 
        // whether object is null or not
        if (i1 == null) {
            System.out.println("Given object i1 is null");
            System.out.println("The value of the object i1 is " + i1);
        } else {
            System.out.println("Given object i1 is not null");
            System.out.println("The value of the object i1 is " + i1);
        }

        // We created an integer object with specified value
        Integer i2 = 100;

        // By using == operator to compare two objects and 
        // with the help of null we will be easily identify 
        // whether object is null or not
        if (i2 == null) {
            System.out.println("Given object i2 is null");
            System.out.println("The value of the object i2 is " + i2);
        } else {
            System.out.println("Given object i2 is not null");
            System.out.println("The value of the object i2 is " + i2);
        }
    }
}

Sortie

D:\Programs>javac ToCheckNullObject.java

D:\Programs>java ToCheckNullObject
Given object str1 is null
The value of the object str1 is null
Given object str2 is not null
The value of the object str2 is Welcome in Java World
Given object str3 is not null
The value of the object str3 is
Given object i1 is null
The value of the object i1 is null
Given object i2 is not null
The value of the object i2 is 100

Balise Java