Java Null Check: How to Test for Null Objects

In Java, you can check if an object is null using the following ways:

Object obj = null;

if(obj == null) {
    System.out.println("对象为空");
} else {
    System.out.println("对象不为空");
}

In this example, first set the obj object to null, then use an if statement to check if the object is empty. If the object is null, print “Object is empty”; if the object is not null, print “Object is not empty”.

bannerAds