Java in the list == null and list.size () == 0 difference

1.list == null, means the list has not even address does not exist in a heap.

2.list.size () = 0, meaning that the stack has a list, but not yet put element, whose length varies with changes in the number of elements, zero temporarily.

3.list If null, it indicates not initialized. This is a list of calling any method will throw an empty exception. list.size () == 0 has been described new list before, but there is no value.

4. The judge must pay attention to the order, even if the cup (list) are not directly determine whether there is water (list.size ()), will report NullPointerException exception.

For example: I have an empty cup (list), but you do not have cup, then you are null, my size is zero. You want to hold water will need to buy a cup (new ArrayList ();), but I can hold water (list.add (water)) directly. If you just hold water, because there is no glass, the water will flow (null pointer exception). Once every so often (list == null) && (list.size () == 0) is determined.

Guess you like

Origin www.cnblogs.com/daijiabao/p/11183241.html