fromIntegerOrNull
Returns a NonNegativeInteger representing the specified value, or returns null if value is negative.
Calling from Kotlin
Here's an example of calling this function from Kotlin code:
val value: Integer = Integer.fromLong(42)
val result: NonNegativeInteger = NonNegativeInteger.fromInteger(value)
check("$result" == "42")
val negative: Integer = Integer.fromLong(-1)
val exception: Throwable? = runCatching {
NonNegativeInteger.fromInteger(negative)
}.exceptionOrNull()
check(exception is IllegalArgumentException)
val safeResult: NonNegativeInteger? =
NonNegativeInteger.fromIntegerOrNull(negative)
check(safeResult == null)Content copied to clipboard
This function is hidden from Java, because nullability is not explicit in its type system.
See the fromInteger function for throwing an exception instead of returning null when value is negative.