Skip to main content
Int in Kotlin is a built-in numeric type representing a 32-bit (4-byte) signed two’s complement integer. It is a member of the kotlin package, inherits from the abstract Number class, and implements the Comparable<Int> interface.

Technical Specifications

  • Memory Size: 32 bits (4 bytes)
  • Minimum Value (Int.MIN_VALUE): -2,147,483,648 (231-2^{31})
  • Maximum Value (Int.MAX_VALUE): 2,147,483,647 (23112^{31}-1)
  • JVM Representation: On the Java Virtual Machine, a non-nullable Int compiles directly to the Java primitive int.

Syntax and Instantiation

Kotlin infers the Int type for any whole number literal that falls within the 32-bit range unless explicitly specified otherwise. Literals can be expressed in decimal, hexadecimal, or binary formats. Octal literals are not supported.

Nullability and Boxing

Kotlin distinguishes between non-nullable (Int) and nullable (Int?) types. This distinction dictates how the compiler allocates memory on the JVM.
  • Int: Compiled to the unboxed primitive int to optimize performance and memory.
  • Int?: Compiled to the boxed wrapper class java.lang.Integer. Boxing also occurs when an Int is used as a generic type argument (e.g., List<Int>).
Because boxing allocates objects in memory, referential equality (===) behaves differently on nullable types compared to structural equality (==). Using === on non-nullable Int types is deprecated as identity equality is meaningless for unboxed primitives.

Explicit Type Conversion

Kotlin does not support implicit widening conversions. An Int cannot be implicitly assigned to a Long or Double variable. Conversion must be handled explicitly using member functions inherited and overridden from the abstract Number class.

Bitwise Operations

Kotlin does not use standard C-style bitwise operators (e.g., <<, >>, &, |). Instead, bitwise operations on Int are performed using named functions. Binary bitwise operations are invoked as infix functions, whereas bitwise inversion (inv()) is a unary function that takes no arguments.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More