Added in API level 19

Objects

class Objects
kotlin.Any
   ↳ java.util.Objects

This class consists of static utility methods for operating on objects, or checking certain conditions before operation. These utilities include null-safe or null-tolerant methods for computing the hash code of an object, returning a string for an object, comparing two objects, and checking if indexes or sub-range values are out of bounds.

Summary

Public methods
static Int
checkFromIndexSize(fromIndex: Int, size: Int, length: Int)

Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

static Long
checkFromIndexSize(fromIndex: Long, size: Long, length: Long)

Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

static Int
checkFromToIndex(fromIndex: Int, toIndex: Int, length: Int)

Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

static Long
checkFromToIndex(fromIndex: Long, toIndex: Long, length: Long)

Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

static Int
checkIndex(index: Int, length: Int)

Checks if the index is within the bounds of the range from 0 (inclusive) to length (exclusive).

static Long
checkIndex(index: Long, length: Long)

Checks if the index is within the bounds of the range from 0 (inclusive) to length (exclusive).

static Int
compare(a: T, b: T, c: Comparator<in T>)

Returns 0 if the arguments are identical and c.compare(a, b) otherwise.

static Boolean
deepEquals(a: Any?, b: Any?)

Returns true if the arguments are deeply equal to each other and false otherwise.

static Boolean
equals(a: Any?, b: Any?)

Returns true if the arguments are equal to each other and false otherwise.

static Int
hash(vararg values: Any!)

Generates a hash code for a sequence of input values.

static Int

Returns the hash code of a non-null argument and 0 for a null argument.

static Boolean
isNull(obj: Any?)

Returns true if the provided reference is null otherwise returns false.

static Boolean
nonNull(obj: Any?)

Returns true if the provided reference is non-null otherwise returns false.

static T
requireNonNull(obj: T?)

Checks that the specified object reference is not null.

static T
requireNonNull(obj: T?, message: String)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

static T
requireNonNull(obj: T?, messageSupplier: Supplier<String!>)

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

static T
requireNonNullElse(obj: T?, defaultObj: T)

Returns the first argument if it is non-null and otherwise returns the non-null second argument.

static T
requireNonNullElseGet(obj: T?, supplier: Supplier<out T>)

Returns the first argument if it is non-null and otherwise returns the non-null value of supplier.get().

static String

{@return a string equivalent to the string returned by {@code * Object.

static String

Returns the result of calling toString for a non-null argument and "null" for a null argument.

static String
toString(o: Any?, nullDefault: String)

Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

Public methods

checkFromIndexSize

Added in API level 30
static fun checkFromIndexSize(
    fromIndex: Int,
    size: Int,
    length: Int
): Int

Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

The sub-range is defined to be out of bounds if any of the following inequalities is true:

  • fromIndex < 0
  • size < 0
  • fromIndex + size > length, taking into account integer overflow
  • length < 0, which is implied from the former inequalities
Parameters
fromIndex Int: the lower-bound (inclusive) of the sub-interval
size Int: the size of the sub-range
length Int: the upper-bound (exclusive) of the range
Return
Int fromIndex if the sub-range within bounds of the range
Exceptions
java.lang.IndexOutOfBoundsException if the sub-range is out of bounds

checkFromIndexSize

Added in API level 34
static fun checkFromIndexSize(
    fromIndex: Long,
    size: Long,
    length: Long
): Long

Checks if the sub-range from fromIndex (inclusive) to fromIndex + size (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

The sub-range is defined to be out of bounds if any of the following inequalities is true:

  • fromIndex < 0
  • size < 0
  • fromIndex + size > length, taking into account integer overflow
  • length < 0, which is implied from the former inequalities
Parameters
fromIndex Long: the lower-bound (inclusive) of the sub-interval
size Long: the size of the sub-range
length Long: the upper-bound (exclusive) of the range
Return
Long fromIndex if the sub-range within bounds of the range
Exceptions
java.lang.IndexOutOfBoundsException if the sub-range is out of bounds

checkFromToIndex

Added in API level 30
static fun checkFromToIndex(
    fromIndex: Int,
    toIndex: Int,
    length: Int
): Int

Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

The sub-range is defined to be out of bounds if any of the following inequalities is true:

  • fromIndex < 0
  • fromIndex > toIndex
  • toIndex > length
  • length < 0, which is implied from the former inequalities
Parameters
fromIndex Int: the lower-bound (inclusive) of the sub-range
toIndex Int: the upper-bound (exclusive) of the sub-range
length Int: the upper-bound (exclusive) the range
Return
Int fromIndex if the sub-range within bounds of the range
Exceptions
java.lang.IndexOutOfBoundsException if the sub-range is out of bounds

checkFromToIndex

Added in API level 34
static fun checkFromToIndex(
    fromIndex: Long,
    toIndex: Long,
    length: Long
): Long

Checks if the sub-range from fromIndex (inclusive) to toIndex (exclusive) is within the bounds of range from 0 (inclusive) to length (exclusive).

The sub-range is defined to be out of bounds if any of the following inequalities is true:

  • fromIndex < 0
  • fromIndex > toIndex
  • toIndex > length
  • length < 0, which is implied from the former inequalities
Parameters
fromIndex Long: the lower-bound (inclusive) of the sub-range
toIndex Long: the upper-bound (exclusive) of the sub-range
length Long: the upper-bound (exclusive) the range
Return
Long fromIndex if the sub-range within bounds of the range
Exceptions
java.lang.IndexOutOfBoundsException if the sub-range is out of bounds

checkIndex

Added in API level 30
static fun checkIndex(
    index: Int,
    length: Int
): Int

Checks if the index is within the bounds of the range from 0 (inclusive) to length (exclusive).

The index is defined to be out of bounds if any of the following inequalities is true:

  • index < 0
  • index >= length
  • length < 0, which is implied from the former inequalities
Parameters
index Int: the index
length Int: the upper-bound (exclusive) of the range
Return
Int index if it is within bounds of the range
Exceptions
java.lang.IndexOutOfBoundsException if the index is out of bounds

checkIndex

Added in API level 34
static fun checkIndex(
    index: Long,
    length: Long
): Long

Checks if the index is within the bounds of the range from 0 (inclusive) to length (exclusive).

The index is defined to be out of bounds if any of the following inequalities is true:

  • index < 0
  • index >= length
  • length < 0, which is implied from the former inequalities
Parameters
index Long: the index
length Long: the upper-bound (exclusive) of the range
Return
Long index if it is within bounds of the range
Exceptions
java.lang.IndexOutOfBoundsException if the index is out of bounds

compare

Added in API level 19
static fun <T : Any!> compare(
    a: T,
    b: T,
    c: Comparator<in T>
): Int

Returns 0 if the arguments are identical and c.compare(a, b) otherwise. Consequently, if both arguments are null 0 is returned.

Note that if one of the arguments is null, a NullPointerException may or may not be thrown depending on what ordering policy, if any, the Comparator chooses to have for null values.

Parameters
<T> the type of the objects being compared
a T: an object
b T: an object to be compared with a
c Comparator<in T>: the Comparator to compare the first two arguments
Return
Int 0 if the arguments are identical and c.compare(a, b) otherwise.

deepEquals

Added in API level 19
static fun deepEquals(
    a: Any?,
    b: Any?
): Boolean

Returns true if the arguments are deeply equal to each other and false otherwise. Two null values are deeply equal. If both arguments are arrays, the algorithm in Arrays.deepEquals is used to determine equality. Otherwise, equality is determined by using the equals method of the first argument.

Parameters
a Any?: an object
b Any?: an object to be compared with a for deep equality
Return
Boolean true if the arguments are deeply equal to each other and false otherwise

equals

Added in API level 19
static fun equals(
    a: Any?,
    b: Any?
): Boolean

Returns true if the arguments are equal to each other and false otherwise. Consequently, if both arguments are null, true is returned. Otherwise, if the first argument is not null, equality is determined by calling the equals method of the first argument with the second argument of this method. Otherwise, false is returned.

Parameters
a Any?: an object
b Any?: an object to be compared with a for equality
Return
Boolean true if the arguments are equal to each other and false otherwise

hash

Added in API level 19
static fun hash(vararg values: Any!): Int

Generates a hash code for a sequence of input values. The hash code is generated as if all the input values were placed into an array, and that array were hashed by calling java.util.Arrays#hashCode(java.lang.Object[]).

This method is useful for implementing java.lang.Object#hashCode() on objects containing multiple fields. For example, if an object that has three fields, x, y, and z, 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 calling hashCode(java.lang.Object).
Parameters
values Any!: the values to be hashed
Return
Int a hash value of the sequence of input values

hashCode

Added in API level 19
static fun hashCode(o: Any?): Int

Returns the hash code of a non-null argument and 0 for a null argument.

Parameters
o Any?: an object
Return
Int the hash code of a non-null argument and 0 for a null argument

isNull

Added in API level 24
static fun isNull(obj: Any?): Boolean

Returns true if the provided reference is null otherwise returns false.

Parameters
obj Any?: a reference to be checked against null
Return
Boolean true if the provided reference is null otherwise false

nonNull

Added in API level 24
static fun nonNull(obj: Any?): Boolean

Returns true if the provided reference is non-null otherwise returns false.

Parameters
obj Any?: a reference to be checked against null
Return
Boolean true if the provided reference is non-null otherwise false

requireNonNull

Added in API level 19
static fun <T : Any!> requireNonNull(obj: T?): T

Checks that the specified object reference is not 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);
  }
  

Parameters
obj T?: the object reference to check for nullity
<T> the type of the reference
Return
T obj if not null
Exceptions
java.lang.NullPointerException if obj is null

requireNonNull

Added in API level 19
static fun <T : Any!> requireNonNull(
    obj: T?,
    message: String
): T

Checks that the specified object reference is not null and throws a customized NullPointerException 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");
  }
  

Parameters
obj T?: the object reference to check for nullity
message String: detail message to be used in the event that a NullPointerException is thrown
<T> the type of the reference
Return
T obj if not null
Exceptions
java.lang.NullPointerException if obj is null

requireNonNull

Added in API level 24
static fun <T : Any!> requireNonNull(
    obj: T?,
    messageSupplier: Supplier<String!>
): T

Checks that the specified object reference is not null and throws a customized NullPointerException if it is.

Unlike the method requireNonNull(java.lang.Object,java.lang.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.

Parameters
obj T?: the object reference to check for nullity
messageSupplier Supplier<String!>: supplier of the detail message to be used in the event that a NullPointerException is thrown
<T> the type of the reference
Return
T obj if not null
Exceptions
java.lang.NullPointerException if obj is null

requireNonNullElse

Added in API level 30
static fun <T : Any!> requireNonNullElse(
    obj: T?,
    defaultObj: T
): T

Returns the first argument if it is non-null and otherwise returns the non-null second argument.

Parameters
obj T?: an object
defaultObj T: a non-null object to return if the first argument is null
<T> the type of the reference
Return
T the first argument if it is non-null and otherwise the second argument if it is non-null
Exceptions
java.lang.NullPointerException if both obj is null and defaultObj is null

requireNonNullElseGet

Added in API level 30
static fun <T : Any!> requireNonNullElseGet(
    obj: T?,
    supplier: Supplier<out T>
): T

Returns the first argument if it is non-null and otherwise returns the non-null value of supplier.get().

Parameters
obj T?: an object
supplier Supplier<out T>: of a non-null object to return if the first argument is null
<T> the type of the first argument and return type
Return
T the first argument if it is non-null and otherwise the value from supplier.get() if it is non-null
Exceptions
java.lang.NullPointerException if both obj is null and either the supplier is null or the supplier.get() value is null

toIdentityString

static fun toIdentityString(o: Any): String

{@return a string equivalent to the string returned by {@code * Object.toString} if that method and {@code hashCode} are not * overridden}

Parameters
o Any: an object
Exceptions
java.lang.NullPointerException if the argument is null

toString

Added in API level 19
static fun toString(o: Any?): String

Returns the result of calling toString for a non-null argument and "null" for a null argument.

Parameters
o Any?: an object
Return
String the result of calling toString for a non-null argument and "null" for a null argument

toString

Added in API level 19
static fun toString(
    o: Any?,
    nullDefault: String
): String

Returns the result of calling toString on the first argument if the first argument is not null and returns the second argument otherwise.

Parameters
o Any?: an object
nullDefault String: string to return if the first argument is null
Return
String the result of calling toString on the first argument if it is not null and the second argument otherwise.