如何在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 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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值