objects.nonnull
时间: 2024-04-16 19:08:46 浏览: 23
`Objects.nonNull` is a static utility method in Java's `java.util.Objects` class that is used to check if an object reference is not null. It takes an object reference as its argument and throws a `NullPointerException` if the reference is null. If the reference is not null, the method returns the reference unchanged.
It is commonly used in situations where a null check is needed to avoid potential `NullPointerExceptions`. For example:
```
public void doSomething(Object obj) {
Objects.requireNonNull(obj, "Object parameter cannot be null");
// rest of the method logic
}
```
In the above code, if the `obj` parameter is null, a `NullPointerException` will be thrown with the given error message. If `obj` is not null, the method will continue to execute.