Java: How to check for null pointers efficiently

前端 未结 14 1784
清歌不尽
清歌不尽 2020-12-03 01:17

There are some patterns for checking whether a parameter to a method has been given a null value.

First, the classic one. It is common in self-made code

14条回答
  •  爱一瞬间的悲伤
    2020-12-03 01:44

    You can use the Objects Utility Class.

    public void method1(String arg) {
      Objects.requireNonNull(arg);
    }
    

    see http://docs.oracle.com/javase/7/docs/api/java/util/Objects.html#requireNonNull%28T%29

提交回复
热议问题