equals():比较两个对象是否相等
需要注意的是,Objects.equals()方法考虑了对象为null的情况
String password = null;
String repeatPassword = "123456";
// Objects.equals()方法考虑了空指针异常的情况
System.out.println(Objects.equals(password, repeatPassword));
// 字符串的equals()方法没有考虑空指针异常的情况
System.out.println(password.equals(repeatPassword));
查看源码可知,Objects.equals()只加了空指针判断,其他与常规的equals()一样
isNull():判断对象是否为空
String username = "root";
String password = null;
// 判断对象是否为空
System.out.println(Objects.isNull(username));
System.out.println(Objects.isNull(password));
nonNull():判断对象是否不是空,相当于!isNull()
String username = "root";
String password = null;
// 判断对象是否不为空,相当于!isNull()
System.out.println(Objects.nonNull(username));
System.out.println(Objects.nonNull(password));