To give you an example list==null means you don’t have a cup size==0 means you have a cup, but there is no water in the cup If you want to drink water, of course you must have a cup, and there is water in the cup So double judgment is required
First determine whether the object is empty. This condition is only true if the object is not empty and the size of the list is greater than 0. If your object = null, then when you get the size, it must be a null pointer.
list==null means that the object has not been instantiated, list.size()>0 means that the list object cannot contain only one piece of data, and null does not contain size(), it is equal to 0. These are two concepts
list is not null, but list.size() is indeed equal to 0
To give you an example
list==null means you don’t have a cup
size==0 means you have a cup, but there is no water in the cup
If you want to drink water, of course you must have a cup, and there is water in the cup
So double judgment is required
Please try to answer the following questions:
If it’s not equal to null, why can’t it be equal to zero?
What is the difference between null and object?
What is the difference between null and empty list?
How did the much-criticized nullpointer exception come about?
First determine whether the object is empty. This condition is only true if the object is not empty and the size of the list is greater than 0. If your object = null, then when you get the size, it must be a null pointer.
plist != null Filter this: List plist = null;
plist.size() > 0 Filter this: List plist = new ArrayList();
list==null means that the object has not been instantiated, list.size()>0 means that the list object cannot contain only one piece of data, and null does not contain size(), it is equal to 0. These are two concepts
One means that the car has not been built, and the other means that there is a car but no cargo has been loaded
This way of writing is actually not good enough. The recommended way of writing is this:
Which object is there but no data