Objects.requireNonNull方法
判断一个对象是否为空,如果为空则抛出一个空指针,下面是方法的定义,用法就不用我说了.(如果不清楚就想想java基础的方法定义和调用).
/**
* Checks that the specified object reference is not {@code null}. This
* method is designed primarily for doing parameter validation in methods
* and constructors, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar) {
* this.bar = Objects.requireNonNull(bar);
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
本文深入解析Java中Objects.requireNonNull方法的使用及作用。此方法用于检查参数是否为null,若为null则抛出NullPointerException,适用于方法和构造函数参数验证。
3977

被折叠的 条评论
为什么被折叠?



