short

About

  • Definition: short is one of the eight primitive data types in Java used to store integer values with a smaller range compared to int. It is particularly useful when memory usage is a concern and the values fit within its range.

  • Size: Occupies 2 bytes (16 bits) in memory.

  • Value Range: -32,768 to 32,767 (-2^15 to 2^15 - 1).

  • Default Value: The default value of short is 0.

  • Wrapper Class: The wrapper class for short is Short, located in java.lang.

Characteristics

  1. Signed Integer Representation: The short type is signed, meaning it can represent both positive and negative integers.

  2. Compact Data Type: Compared to int, it saves memory, especially useful for large arrays of small numbers.

  3. Arithmetic Operations: Like other numeric types, short supports arithmetic operations such as addition, subtraction, multiplication, and division.

  4. Promotion in Expressions: In arithmetic expressions, short values are automatically promoted to int before any operation. Example:

    short a = 10;
    short b = 20;
    int result = a + b; // Promoted to int
  5. Usage in Streams: Since Java Streams operate on int, short values must be converted or boxed to work with streams.

  6. Interoperability: Often used in file handling, network protocols, and legacy systems where compact data representation is required.

  7. Memory Usage: Requires 16 bits (2 bytes) per value, stored in signed 2’s complement representation.

Operations with short

Arithmetic and Logical Operations

Operation

Example

Description

Arithmetic Operations

short a = 10 + 20;

Addition, subtraction, multiplication, division.

Comparison Operations

a > b

Compares two short values.

Casting

(short) largeValue

Explicitly cast larger types like int to short.

Conversion Methods

Conversion

Method

Example

short to String

String.valueOf(short)

String.valueOf(100)"100"

String to short

Short.parseShort(String)

Short.parseShort("123")123

short to int

Implicit conversion

int value = shortVar;

int to short

Explicit cast (short)

(short) 50000 → Overflow behavior

short to double

Implicit conversion

double value = shortVar;

Short Wrapper Class (Short)

Method

Description

Short.valueOf(short s)

Returns a Short instance for the given short value.

Short.parseShort(String s)

Parses the string argument as a short.

Short.toString(short s)

Converts short to its String representation.

Short.compare(short x, y)

Compares two short values.

Short.MIN_VALUE

Minimum value of short (-32,768).

Short.MAX_VALUE

Maximum value of short (32,767).

Common Mistakes

  1. Overflow Issues: Casting a larger integer to short can cause data loss due to overflow.

    int largeValue = 70000;
    short overflowed = (short) largeValue; // Value wraps around
  2. Automatic Promotion: Arithmetic operations promote short to int, which can lead to unexpected results.

    short a = 10;
    short b = 20;
    short c = a + b; // Compilation error (needs cast)

Examples

Basic example

short num1 = 100;
short num2 = 200;

// Arithmetic operations
short sum = (short) (num1 + num2); // Explicit cast required as + results in int type
System.out.println("Sum: " + sum); // Sum: 300
short a = 10 + 20; // 30

// Comparing values
System.out.println("Is num1 greater than num2? " + (num1 > num2)); // false

// Casting
int largerValue = 50000;
short castedValue = (short) largerValue; // Overflow occurs
System.out.println("Casted Value: " + castedValue); // -15536

Using short in Arrays

public class ShortArrayExample {
    public static void main(String[] args) {
        short[] numbers = {10, 20, 30, 40, 50};

        for (short num : numbers) {
            System.out.println(num);
        }
    }
}

Converting short to String

public class ShortConversion {
    public static void main(String[] args) {
        short num = 25;
        String str = Short.toString(num);
        System.out.println("Short as String: " + str);
    }
}

Using short with Streams

import java.util.stream.IntStream;

public class ShortStreamExample {
    public static void main(String[] args) {
        short[] numbers = {10, 20, 30};

        IntStream.range(0, numbers.length)
                 .map(i -> numbers[i])
                 .forEach(System.out::println);
    }
}

Short as Bit Fields

public class ShortBitExample {
    public static void main(String[] args) {
        short value = 0b0101; // Binary representation
        System.out.println("Bitwise AND: " + (value & 0b0011)); // 0001
    }
}

Handling Large Short Arrays

public class ShortArrayMemory {
    public static void main(String[] args) {
        short[] largeArray = new short[1_000_000]; // 2 MB memory
        System.out.println("Array Length: " + largeArray.length);
    }
}

The number 1_000_000 is 1 million, and the underscores (_) in numeric literals are a feature introduced in Java 7 to improve readability of numbers.

Wrapper Class Example

public class ShortWrapperExample {
    public static void main(String[] args) {
        String strValue = "1234";
        short parsedValue = Short.parseShort(strValue);

        System.out.println("Parsed Value: " + parsedValue);

        short minValue = Short.MIN_VALUE;
        short maxValue = Short.MAX_VALUE;

        System.out.println("Min Value: " + minValue);
        System.out.println("Max Value: " + maxValue);
    }
}

Last updated

Was this helpful?