public final classObjectsextendsObject
static utility methods for operating on objects. These utilities includenull-safe ornull-tolerant methods for computing the hash code of an object, returning a string for an object, and comparing two objects.| Modifier and Type | Method | Description |
|---|---|---|
static <T> int | compare(T a, T b,Comparator<? super T> c) | Returns 0 if the arguments are identical and c.compare(a, b) otherwise. |
static boolean | deepEquals(Object a,Object b) | Returns true if the arguments are deeply equal to each other andfalse otherwise. |
static boolean | equals(Object a,Object b) | Returns true if the arguments are equal to each other andfalse otherwise. |
static int | hash(Object... values) | Generates a hash code for a sequence of input values. |
static int | hashCode(Object o) | Returns the hash code of a non- null argument and 0 for anull argument. |
static boolean | isNull(Object obj) | Returns true if the provided reference isnull otherwise returnsfalse. |
static boolean | nonNull(Object obj) | Returns true if the provided reference is non-null otherwise returnsfalse. |
static <T> T | requireNonNull(T obj) | Checks that the specified object reference is not null. |
static <T> T | requireNonNull(T obj,String message) | Checks that the specified object reference is not null and throws a customizedNullPointerException if it is. |
static <T> T | requireNonNull(T obj,Supplier<String> messageSupplier) | Checks that the specified object reference is not null and throws a customizedNullPointerException if it is. |
staticString | toString(Object o) | Returns the result of calling toString for a non-null argument and"null" for anull argument. |
staticString | toString(Object o,String nullDefault) | Returns the result of calling toString on the first argument if the first argument is notnull and returns the second argument otherwise. |
public static boolean equals(Object a,Object b)
true if the arguments are equal to each other andfalse otherwise. Consequently, if both arguments arenull,true is returned and if exactly one argument isnull,false is returned. Otherwise, equality is determined by using theequals method of the first argument.a - an objectb - an object to be compared witha for equalitytrue if the arguments are equal to each other andfalse otherwiseObject.equals(Object)public static boolean deepEquals(Object a,Object b)
true if the arguments are deeply equal to each other andfalse otherwise. Twonull values are deeply equal. If both arguments are arrays, the algorithm inArrays.deepEquals is used to determine equality. Otherwise, equality is determined by using theequals method of the first argument.a - an objectb - an object to be compared witha for deep equalitytrue if the arguments are deeply equal to each other andfalse otherwiseArrays.deepEquals(Object[], Object[]),equals(Object, Object)public static int hashCode(Object o)
null argument and 0 for anull argument.o - an objectnull argument and 0 for anull argumentObject.hashCode()public static int hash(Object... values)
Arrays.hashCode(Object[]).This method is useful for implementingObject.hashCode() on objects containing multiple fields. For example, if an object that has three fields,x,y, andz, one could write:
@Override public int hashCode() { return Objects.hash(x, y, z); }Warning: When a single object reference is supplied, the returned value does not equal the hash code of that object reference. This value can be computed by callinghashCode(Object).values - the values to be hashedArrays.hashCode(Object[]),List.hashCode()public static String toString(Object o)
toString for a non-null argument and"null" for anull argument.o - an objecttoString for a non-null argument and"null" for anull argumentObject.toString(),String.valueOf(Object)public static String toString(Object o,String nullDefault)
toString on the first argument if the first argument is notnull and returns the second argument otherwise.o - an objectnullDefault - string to return if the first argument isnulltoString on the first argument if it is notnull and the second argument otherwise.toString(Object)public static <T> int compare(T a, T b,Comparator<? super T> c)
c.compare(a, b) otherwise. Consequently, if both arguments arenull 0 is returned.Note that if one of the arguments isnull, aNullPointerException may or may not be thrown depending on what ordering policy, if any, theComparator chooses to have fornull values.
T - the type of the objects being compareda - an objectb - an object to be compared withac - theComparator to compare the first two argumentsc.compare(a, b) otherwise.Comparable,Comparatorpublic static <T> T requireNonNull(T obj)
null. This method is designed primarily for doing parameter validation in methods and constructors, as demonstrated below: public Foo(Bar bar) { this.bar = Objects.requireNonNull(bar); }T - the type of the referenceobj - the object reference to check for nullityobj if notnullNullPointerException - ifobj isnullpublic static <T> T requireNonNull(T obj,String message)
null and throws a customizedNullPointerException if it is. This method is designed primarily for doing parameter validation in methods and constructors with multiple parameters, as demonstrated below: public Foo(Bar bar, Baz baz) { this.bar = Objects.requireNonNull(bar, "bar must not be null"); this.baz = Objects.requireNonNull(baz, "baz must not be null"); }T - the type of the referenceobj - the object reference to check for nullitymessage - detail message to be used in the event that aNullPointerException is thrownobj if notnullNullPointerException - ifobj isnullpublic static boolean isNull(Object obj)
true if the provided reference isnull otherwise returnsfalse.public static boolean nonNull(Object obj)
true if the provided reference is non-null otherwise returnsfalse.public static <T> T requireNonNull(T obj,Supplier<String> messageSupplier)
null and throws a customizedNullPointerException if it is.Unlike the methodrequireNonNull(Object, String), this method allows creation of the message to be deferred until after the null check is made. While this may confer a performance advantage in the non-null case, when deciding to call this method care should be taken that the costs of creating the message supplier are less than the cost of just creating the string message directly.
T - the type of the referenceobj - the object reference to check for nullitymessageSupplier - supplier of the detail message to be used in the event that aNullPointerException is thrownobj if notnullNullPointerException - ifobj isnull