learning java Objects.requireNonNull 当传入参数为null时,该方法返回参数本身
System.out.println(Objects.hashCode(obj)); System.out.println(Objects.toString(obj)); System.out.println(Objects.requireNonNull(obj, "obj参数不能为空"));
output:
0 null Exception in thread "main" java.lang.NullPointerException: obj参数不能为空 at java.base/java.util.Objects.requireNonNull(Objects.java:246) at ObjectTest.main(ObjectTest.java:9)