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.
如果两个引用(对象)都指向相同的内存位置,则“ ==”运算符将返回true;否则,如果两个对象都指向不同的内存位置,则它将返回false。
null is a keyword introduced in java which is used to check whether an object is null or not.
null是java中引入的关键字,用于检查对象是否为null。
Meaning of null in a different form is "no object" or "unknown".
null的不同形式含义是“ no object”或“ 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
翻译自: https://www.includehelp.com/java/how-to-check-an-object-is-null-in-java.aspx