Compare two objects with a check for null

前端 未结 7 1155
鱼传尺愫
鱼传尺愫 2020-12-10 00:21

Is there a method in the JDK that compares two objects for equality, accounting for nulls? Something like this:

public static boolean equals(Object o1, Obje         


        
7条回答
  •  时光说笑
    2020-12-10 01:14

    Apache Commons Lang has such a method: ObjectUtils.equals(object1, object2). You don't want generics on such a method, it will lead to bogus compilation errors, at least in general use. Equals knows very well (or should - it is part of the contract) to check the class of the object and return false, so it doesn't need any additional type safety.

提交回复
热议问题