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