如何在Java中检查对象是否为空?
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 nullString 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 notif (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 valueString 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 notif (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 valueString str3 = " ";// By using == operator to compare two objects and // with the help of null we will be easily identify // whether object is null or notif (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 nullInteger 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 notif (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 valueInteger 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 notif (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.javaD:\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
總結
以上是生活随笔為你收集整理的如何在Java中检查对象是否为空?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 地下城堡魂之诗兄妹成就的完成方法是什么
- 下一篇: dbms支持哪几种数据模型_DBMS中不