The first tells you whether the list
variable has been assigned a List instance or not.
The second tells you if the List referenced by the list
variable is empty.
If list
is null, the second line will throw a NullPointerException
.
If you want to so something only when the list is empty, it is safer to write :
if (list != null && list.isEmpty()) { do something }
If you want to do something if the list is either null or empty, you can write :
if (list == null || list.isEmpty()) { do something }
If you want to do something if the list is not empty, you can write :
if (list != null && !list.isEmpty()) { do something }
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…