Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

short — signed 16-bit integer type

short is a signed primitive integer type ranging from -32768 to 32767. Learn how it differs from the Short wrapper class, why arithmetic promotes it to int, and how narrowing can lose information.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

short name = value;
Short boxed = name;
short unboxed = boxed;

short is a keyword naming a primitive integer type: a signed 16-bit two's-complement integer. Its range is -32768 through 32767, inclusive. It cannot store a fractional part or hold null.

The declaration short value = 32767; is valid: an int constant expression within range can be assigned without a cast. When adding two short values, however, the operands are promoted to int, so the result has type int. Casting from int with (short) keeps only the lowest 16 bits; it does not check the range and can change the sign.

Short is the wrapper class java.lang.Short, not an alternative spelling of the primitive type. It enables these values to be used as objects, for example in List<Short>. Assigning short to Short performs boxing; the reverse performs unboxing. A Short reference can be null; unboxing it then throws NullPointerException.

In the example, value + 1 produces the int value 32768. Explicit narrowing produces -32768, while boxing and unboxing preserve the original 32767. The example targets Java SE 21; it does not attest to the production runtime version.

Examples

public class ShortDemo {
    public static void main(String[] args) {
        short value = 32767;
        int sum = value + 1;
        short narrowed = (short) sum;
        Short boxed = value;
        short unboxed = boxed;

        System.out.println(sum);
        System.out.println(narrowed);
        System.out.println(unboxed);
    }
}

Result

32768
-32768
32767

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources