Skip to main content
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.

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.

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.

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.
Tired of Poor Kotlin Skills? Fix That With Deep Grasping!Learn More