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.

Long is a built-in numeric data type in Kotlin representing a 64-bit signed two’s complement integer. It accommodates values ranging from -9,223,372,036,854,775,808 (-263) to 9,223,372,036,854,775,807 (263 - 1).

Instantiation and Syntax

When assigning an integer literal to a variable explicitly typed as Long, the compiler automatically instantiates the literal as a Long. If the variable’s type is not explicitly declared, appending an uppercase L suffix forces the compiler to infer the type as Long. Additionally, any integer literal exceeding the maximum value of a 32-bit Int is automatically inferred as a Long. Kotlin strictly rejects the lowercase l suffix to prevent visual confusion with the digit 1.
val explicitLong: Long = 42    // Automatically instantiated as Long due to explicit type
val inferredLong = 42L         // 'L' suffix forces Long inference
val largeInferred = 3000000000 // Automatically inferred as Long because it exceeds Int.MAX_VALUE

// Supported literal formats
val decLong = 1_000_000_000_000L // Underscores permitted for readability
val hexLong = 0x0DE0B6B3A7640000L
val binLong = 0b110100101011L
// Note: Octal literals are not supported in Kotlin

Memory Representation and Boxing

When targeting the JVM, Kotlin optimizes memory by compiling a non-nullable Long directly to the Java primitive long. However, if the variable is declared as nullable (Long?) or used in a generic context (e.g., List<Long>), the compiler boxes the value into a java.lang.Long object. This boxing process allocates memory on the heap and introduces object overhead.
val primitive: Long = 100L    // Compiles to primitive 'long'
val boxed: Long? = 100L       // Compiles to 'java.lang.Long'

Type Conversion

While Kotlin automatically adapts integer literals to an explicitly declared Long type, it enforces strict type safety for variables and does not support implicit widening conversions. You cannot directly assign an existing Int, Short, or Byte variable to a Long variable. Explicit conversion using the toLong() function is mandatory.
val intValue: Int = 1024
// val invalidLong: Long = intValue // Compilation error: Type mismatch
val validLong: Long = intValue.toLong()
Conversely, converting a Long to a smaller numeric type (like Int) requires toInt(). If the Long value exceeds the target type’s maximum capacity, the resulting value is truncated to the least significant bits.

Bitwise Operations

Kotlin does not use standard C-style bitwise operators (e.g., <<, |, &). Instead, Long supports bitwise manipulation through named infix functions and standard functions. These operate directly on the 64-bit binary representation.
val base: Long = 0b0010L

// Infix functions
val shiftedLeft = base shl 2      // Shift left (0b1000L)
val shiftedRight = base shr 1     // Signed shift right (0b0001L)
val unsignedShift = base ushr 1   // Unsigned shift right (zero-fills MSB)

val bitwiseAnd = base and 0b0011L // Bitwise AND
val bitwiseOr = base or 0b0100L   // Bitwise OR
val bitwiseXor = base xor 0b1111L // Bitwise XOR

// Standard function (takes no arguments, cannot be called as infix)
val inverted = base.inv()         // Bitwise inversion (flips all 64 bits)
Master Kotlin with Deep Grasping Methodology!Learn More