Java: Clean way of avoiding NullPointerException in equals checks

前端 未结 8 1576
感情败类
感情败类 2021-02-12 12:27

I have an address object that I want to create an equals method for. I could have made this quite simple by doing something like the following (shortened a bit):



        
8条回答
  •  盖世英雄少女心
    2021-02-12 12:37

    You can use a helper method like

    public static boolean isEqual(Object o1, Object o2) {
        return o1 == o2 || (o1 != null && o1.equals(o2));
    }
    

提交回复
热议问题