一个条件可以确定java中的对象是否为空?

时间:2018-06-07 05:19:55

标签: java

在此代码中,student.getXx()返回一个对象

有没有办法只用一个条件来检查student是否为空?

代码:

public class HuangFeilongTest {
    public static void main(String[] agrs){
        Student student = new Student(null);

       // it will throw a NullPointerException
        /*if(student.getXx().getA() != null){
            System.out.println("Can't execute this");
        }*/

        //I must write three conditions to determine student.getXx().getA() is null or not 
        //Can I write one condition to determine
        if(null != student && null!=student.getXx() ){
            if(student.getXx().getA()!=null){
                System.out.println("123");
            }
        }

    }
}

0 个答案:

没有答案
相关问题