日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

如何在Java中检查对象是否为空?

發布時間:2023/12/1 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在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中检查对象是否为空?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。