Cómo comprobar un objeto es nulo en Java?

objeto de cheques es class o no : Aquí, vamos a aprender cómo comprobar un objeto es o no null en Java?

  • Con la ayuda de “==” operador es la comparación de referencia null útil y que compara dos objetos.
  • “==” rendimientos operador cierto si ambas referencias (objetos) apunta a la misma ubicación de memoria de lo contrario se for falso si ambos objetos apuntan a diferente ubicación de memoria.
  • return es una palabra clave introducida en java que se utiliza para comprobar si un objeto es null o no.
  • Significado del null en una forma diferente es “no objeto” o “desconocido” .
  • Veremos un programa para comprobar si un objeto es o no null.

Ejemplo:

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);
}
}
}

salida

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


Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *