Integer

@ExperimentalSince(version = KotoolsTypesVersion.V5_1_0)
class Integer

Represents an integer.

Use this type for preventing overflow when performing arithmetic operations with integers, and for consistent behavior across all platforms when dividing an integer by zero.


Motivations

Motivations

Integer overflow

Problem: Adding, subtracting or multiplying Kotlin integer types (Byte, Short, Int and Long) can lead to an overflow, which produces unexpected behavior.

val x = 9223372036854775807
val y = 10
check(x + y == -9223372036854775799) // instead of 9223372036854775817
check(-x - y == 9223372036854775799) // instead of -9223372036854775817
check(x * y == -10L) // instead of 92233720368547758070

Solution: The Integer type can add, subtract or multiply integers without producing an overflow.

val x: Integer = Integer.from(9223372036854775807)
val y: Integer = Integer.from(10)
check(x + y == Integer.fromDecimal("9223372036854775817"))
check(-x - y == Integer.fromDecimal("-9223372036854775817"))
check(x * y == Integer.fromDecimal("92233720368547758070"))

Division by zero

Problem: Performing division and remainder operations by zero on Kotlin integer types have different behavior per platform: throw an ArithmeticException on JVM and Native platforms, and return 0 on JS platform.

// JVM and Native platforms
val quotient: Result<Int> = kotlin.runCatching { 12 / 0 }
val remainder: Result<Int> = kotlin.runCatching { 12 % 0 }
check(quotient.exceptionOrNull() is ArithmeticException)
check(remainder.exceptionOrNull() is ArithmeticException)
// JS platform
check(12 / 0 == 0)
check(12 % 0 == 0)

Solution: Division and remainder operations by zero on Integer type throw an ArithmeticException on all platforms.

// Common code
val x: Integer = Integer.from(12)
val y: Integer = Integer.from(0)
val quotient: Result<Integer> = kotlin.runCatching { x / y }
val remainder: Result<Integer> = kotlin.runCatching { x % y }
check(quotient.exceptionOrNull() is ArithmeticException)
check(remainder.exceptionOrNull() is ArithmeticException)

Key features

Key features

Types

Link copied to clipboard
object Companion

Contains class-level declarations for the Integer type.

Functions

Link copied to clipboard
operator fun compareTo(other: Integer): Int

Compares this integer with the other one for order. Returns a negative number, zero, or a positive number as this integer is less than, equal to, or greater than the other one.

Link copied to clipboard
operator fun div(other: Integer): Integer

Returns the quotient of dividing this integer by the other one, or throws an ArithmeticException if the other integer is zero.

Link copied to clipboard
fun divOrNull(other: Integer): Integer?

Returns the quotient of dividing this integer by the other one, or returns null if the other integer is zero.

Link copied to clipboard
operator override fun equals(other: Any?): Boolean

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

Link copied to clipboard
override fun hashCode(): Int

Returns a hash code value for this integer.

Link copied to clipboard
operator fun minus(other: Integer): Integer

Subtracts the other integer from this one.

Link copied to clipboard
operator fun plus(other: Integer): Integer

Adds the other integer to this one.

Link copied to clipboard
operator fun rem(other: Integer): Integer

Returns the remainder of dividing this integer by the other one, or throws an ArithmeticException if the other integer is zero.

Link copied to clipboard
fun remOrNull(other: Integer): Integer?

Returns the remainder of dividing this integer by the other one, or returns null if the other integer is zero.

Link copied to clipboard
operator fun times(other: Integer): Integer

Multiplies this integer by the other one.

Link copied to clipboard
override fun toString(): String

Returns the decimal string representation of this integer.

Link copied to clipboard
operator fun unaryMinus(): Integer

Returns the negative of this integer.