Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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.
val inferredInt = 42                 // Type inferred as Int
val explicitInt: Int = 42            // Explicit type declaration
val hexInt: Int = 0x2A               // Hexadecimal literal
val binInt: Int = 0b101010           // Binary literal
val readableInt: Int = 1_000_000     // Underscores for readability

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.
val unboxed: Int = 1000      // Primitive int
val boxed: Int? = 1000       // Boxed java.lang.Integer

val boxedA: Int? = 10000
val boxedB: Int? = 10000

// Structural equality compares the underlying values
println(boxedA == boxedB)  // true 

// Referential equality compares memory addresses of the boxed objects
println(boxedA === boxedB) // false (different object references in memory)

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.
val baseInt: Int = 100

val toLong: Long = baseInt.toLong()
val toDouble: Double = baseInt.toDouble()
val toByte: Byte = baseInt.toByte() // Truncates the 24 most significant bits

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.
val a: Int = 0b0101
val b: Int = 0b0011

// Binary operations (Infix)
val shlResult = a shl 1    // Shift left (equivalent to Java <<)
val shrResult = a shr 1    // Shift right (equivalent to Java >>)
val ushrResult = a ushr 1  // Unsigned shift right (equivalent to Java >>>)
val andResult = a and b    // Bitwise AND (equivalent to Java &)
val orResult = a or b      // Bitwise OR (equivalent to Java |)
val xorResult = a xor b    // Bitwise XOR (equivalent to Java ^)

// Unary operation
val invResult = a.inv()    // Bitwise inversion (equivalent to Java ~)
Master Kotlin with Deep Grasping Methodology!Learn More