Java, null list [closed]
How can I verify if a List is null in Java ?
thanks
By if condition:
if (list == null){
// do something
}
List myList = getListFromSomeMethodThatMightReturnNullAlthoughItsBetterToReturnAnEmptyListThenYouWouldntHaveToDoAnyStupidNullChecking();
if (myList == null){
}
A List
instance can't be null
, an instance is always something. A List
type variable can be null
and to test this, use the expression
List<?> list = null;
if (list == null) {System.out.println("I'm null");}
A List
instance can by empty, meaning the list doesn't contain any values. To ways to test this:
if (list.size() == 0) {...}
if (list.isEmpty()) {...}
A List
instance can contain items that represent null
. To find those, iterate through the list:
for(Object o:list)
if (o == null) {...}
If you have a variable myList of type List
, you can do this by:
if(myList == null)
{
}
精彩评论