1.字符串
str == null || str.isEmpty()
str == null || str.length == 0
str==null&&"".equals(str)
isEmpty()源码:
public boolean isEmpty() {
return value.length == 0;
}
2.包装
i==null
3.数组
a==null||a.length==0
4.集合
users==null||users.isEmpty()
users==null||users.size()==0
isEmpty()源码:
public boolean isEmpty() {
return size() == 0;
}
5.对象
food==null
Objects.isNull(food)
6.代码示例
System.out.println("--------------字符串-------------");
String str = "";
String str1=null;
String str2="1";
System.out.println(str == null || str.isEmpty());
System.out.println(str1 == null || str1.isEmpty());
System.out.println(str2 == null || str2.isEmpty());
System.out.println("--------------包装类型-------------");
Integer i=1;
Integer j=null;
System.out.println(i==null);
System.out.println(j==null);
System.out.println("--------------数组类型-------------");
int a[]=null;
int b[]={};
String c[]={""};
System.out.println(a==null||a.length==0);
System.out.println(b==null||b.length==0);
System.out.println(c==null||c.length==0);
System.out.println("--------------集合类型-------------");
List<User> users = new ArrayList<>();
List<User> users1 = null;
List<User> users2 = new ArrayList<>();
User user = new User();
users2.add(user);
System.out.println(users==null||users.isEmpty());
System.out.println(users1==null||users1.isEmpty());
System.out.println(users2==null||users2.isEmpty());
System.out.println("--------------对象类型-------------");
Food food = new Food();
Food food1=null;
System.out.println(Objects.isNull(food));
System.out.println(food==null);
System.out.println(Objects.isNull(food1));
System.out.println(food1==null);