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.

Short is a 16-bit signed two’s complement integer data type in Kotlin. It occupies 2 bytes of memory and represents numerical values strictly within the range of -32,768 to 32,767 (215-2^{15} to 21512^{15}-1).

Memory and Bounds Constants

The Short companion object provides constant properties to access its structural limits:
  • Short.MIN_VALUE: -32768
  • Short.MAX_VALUE: 32767
  • Short.SIZE_BYTES: 2
  • Short.SIZE_BITS: 16

Instantiation and Syntax

Kotlin does not provide a dedicated literal suffix for Short (unlike L for Long or f for Float). Because integer literals are inferred as Int by default, a Short must be instantiated via explicit type declaration or explicit conversion.
// Explicit type declaration (compiler implicitly narrows the Int literal)
val explicitShort: Short = 32000

// Explicit conversion function
val convertedShort: Short = 32000.toShort()

// Hexadecimal and binary literals
val hexShort: Short = 0x7FFF      // 32767
val binShort: Short = 0b01111111  // 127

JVM Representation and Boxing

On the Kotlin/JVM platform, the compiler maps Short types based on nullability:
  1. Non-nullable (Short): Compiles directly to the Java primitive short.
  2. Nullable (Short?): Triggers boxing, compiling to the Java wrapper class java.lang.Short.
val primitive: Short = 100   // Represented as primitive 'short'
val boxed: Short? = 100      // Represented as 'java.lang.Short'

Type Promotion in Arithmetic

Kotlin enforces strict type safety and does not implicitly widen types. When performing arithmetic operations involving Short, the compiler automatically promotes the operands to Int (or Long if the other operand is a Long) to prevent overflow during intermediate calculations. Consequently, the return type of an arithmetic operation between two Short variables is an Int.
val a: Short = 10
val b: Short = 20

// 'result' is inferred as Int
val result = a + b 

// Explicit downcasting is required to assign the result back to a Short
val shortResult: Short = (a + b).toShort()

Bitwise Operations

As of Kotlin 1.5, the standard library provides strict bitwise operations directly for the Short type. The infix functions and, or, and xor, as well as the unary function inv(), can be invoked directly on a Short and will return a Short. However, bitwise shift operations (shl, shr, ushr) are not defined on the Short class. To perform bit shifts, the Short must first be explicitly promoted to an Int.
val a: Short = 0b0011
val b: Short = 0b0101

// Direct bitwise operations returning Short
val bitwiseAnd: Short = a and b
val bitwiseOr: Short = a or b
val bitwiseXor: Short = a xor b

// Unary bitwise inversion returning Short
val inverted: Short = a.inv() 

// Shift operations require promotion to Int
// Invalid: a shl 2 
val shifted: Int = a.toInt() shl 2
Master Kotlin with Deep Grasping Methodology!Learn More