Home »
Java programming language
How to check an object is null in Java?
Checking object is null or not: Here, we are going to learn how to check an object is null or not in Java?
Submitted by Preeti Jain, on July 03, 2019
- With the help of "==" operator is useful for reference comparison and it compares two objects.
- "==" operator returns true if both references (objects) points to the same memory location otherwise it will return false if both objects point to different memory location.
- null is a keyword introduced in java which is used to check whether an object is null or not.
- Meaning of null in a different form is "no object" or "unknown".
- We will see a program to check whether an object is null or not.
Example:
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);
}
}
}
Output
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