String 字符串
//直接用字符串调用方法 str.isEmpty() 如果为null则会抛出异常
if (StringUtils.isEmpty(str)){// "" 和null均判断为空
System.out.println("string为空");
}
对象
//第1种:
if (Objects.isNull(null)){ //null
System.out.println("obj为空");
}
//第2种:
if (StringUtils.isEmpty(null)){
System.out.println("obj为空");
}
- 6
- 7
- 8
数组
//数组需要判断null和长度length(大小size)
if (arr == null || arr.length == 0){
System.out.println("arr为空");
}
- 1
- 2
- 3
- 4
list set用法同list
//isEmpty()方法内部就是判断size大小
if (list.isEmpty() && list == null){
System.out.println("list为空");
}
//使用spring提供的工具类
List list=null;
if (CollectionUtils.isEmpty(list)){
System.out.println("list为空");
}
- 6
- 7
- 8
- 9