toLongOrNull

Returns the Long representation of this integer, or returns null if this integer is out of range for Long (less than Long.MIN_VALUE or greater than Long.MAX_VALUE).


Calling from Kotlin

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

val x: Integer = Integer.fromLong(42)
val result: Long? = x.toLongOrNull()
check(result == 42L)

val outOfRange: Integer =
Integer.fromLong(Long.MAX_VALUE) + Integer.fromLong(1)
check(outOfRange.toLongOrNull() == null)

This function is hidden from Java, because nullability is not explicit in its type system.

See the toLong function for throwing an exception instead of returning null when this integer is out of range for Long.

Since

5.2.0