Why should one use Objects.requireNonNull()?

前端 未结 11 1840
谎友^
谎友^ 2020-12-02 04:06

I have noted that many Java 8 methods in Oracle JDK use Objects.requireNonNull(), which internally throws NullPointerException if the given object

11条回答
  •  我在风中等你
    2020-12-02 04:42

    Null pointer exception is thrown when you access a member of an object which is null at a later point. Objects.requireNonNull() immediately checks the value and throws exception instantly without moving forward and making it easier to detect where the null exception actually occured.

提交回复
热议问题