equals

operator override fun equals(other: Any?): Boolean

Returns true if the other object is an Integer representing the same numeric value as this one, or returns false otherwise.

This function follows the contract of Any.equals.


Calling from Kotlin

Here's an example of calling this function from Kotlin code:

fun checkEquality(integer: Integer, other: Any?) {
check(integer == other)
check(integer.hashCode() == other.hashCode())
}

fun checkDiff(integer: Integer, other: Any?) {
check(integer != other)
check(integer.hashCode() != other.hashCode())
}

checkEquality(
integer = Integer.fromLong(0),
other = Integer.parse("-000")
)
checkEquality(
integer = Integer.fromLong(42),
other = Integer.parse("+00042")
)
checkEquality(
integer = Integer.fromLong(-42),
other = Integer.parse("-0042")
)

checkDiff(integer = Integer.fromLong(0), other = Integer.fromLong(1))
checkDiff(integer = Integer.fromLong(42), other = 42)
checkDiff(integer = Integer.fromLong(-42), other = null)

Calling from Java

Here's an example of calling this function from Java code:

final BiConsumer<Integer, Object> checkEquality = (integer, other) -> {
final boolean equality = Objects.equals(integer, other);
final boolean hashConformity =
Objects.hashCode(integer) == Objects.hashCode(other);
final boolean check = equality && hashConformity;
if (!check) throw new IllegalStateException("Check failed.");
};

final BiConsumer<Integer, Object> checkDiff = (integer, other) -> {
final boolean equality = !Objects.equals(integer, other);
final boolean hashConformity =
Objects.hashCode(integer) != Objects.hashCode(other);
final boolean check = equality && hashConformity;
if (!check) throw new IllegalStateException("Check failed.");
};

checkEquality.accept(Integer.fromLong(0), Integer.parse("-000"));
checkEquality.accept(Integer.fromLong(42), Integer.parse("+00042"));
checkEquality.accept(Integer.fromLong(-42), Integer.parse("-0042"));

checkDiff.accept(Integer.fromLong(0), Integer.fromLong(1));
checkDiff.accept(Integer.fromLong(42), 42);
checkDiff.accept(Integer.fromLong(-42), null);