short

A short is a primitive data type in Java used to store whole numbers (integers).

  • Size: 16 bits (2 bytes)
  • Range: –32,768 to 32,767

Examples:

short count = 20;
short temperature = -9;

Java has four integer primitive types:

  • byte (8-bit)
  • short (16-bit)
  • int (32-bit)
  • long (64-bit)

short is typically used:

  • When values are guaranteed to be small
  • In low-level or memory-sensitive code
  • When working with binary data or older systems

Tip: int is more commonly used unless memory usage really matters.

Primitive Type

short is a primitive type, which means:

  • It stores its value directly in memory
  • It is not an object **
  • It does not store a reference (memory address) to an object

**An object is an instance of a class that stores data and provides methods to work with that data.

Primitive types have no internal state or methods. When you assign a new value, the old value is simply replaced.

short a = 15;
a = 20;  // the value 15 is replaced with 20

Short Wrapper Class

Each primitive type has a wrapper class. For short, the wrapper class is Short (spelt with capital ‘S’ as opposed to lower case ‘s’ for the primitive)

The Short class provides useful constants and static methods.

Note:

  • A constant is a value that cannot change once it has been set. By convention it is written in UPPERCASE
  • A static method belongs to a class, not an object. It can be called without creating an object
  • These methods belong to the Short wrapper class. They cannot be called directly on a primitive short value and will cause a compile-time error if you try.

Constants

ConstantExampleResult
MAX_VALUEShort.MAX_VALUEReturns 32,767
MIN_VALUEShort.MIN_VALUEReturns –32,768
SIZEShort.SIZEReturns 16 (16 bits)
BYTESShort.BYTESReturns 2 (2 bytes)

Conversion Methods

parseShort(String)

short value = Short.parseShort("123");
  • Converts a numeric string into a short
  • Throws a NumberFormatException if the string is not numeric (e.g. "hello")

toString(short)

String text = Short.toString((short) 42);
  • Converts a short value into a String
  • The parameter must be a short, not a string

Comparison Methods

compare(short x, short y)

Short.compare((short)1, (short)2);

compareTo(Short)

Short x = 10;
Short y = 12;

x.compareTo(y);

compare and compareTo return the following values:

  • 0 if values are equal
  • A negative number if x < y
  • A positive number if x > y

equals(Object)

Short x = 10;
Short y = 12;

x.equals(y);
  • Returns true if values are equal
  • Returns false otherwise

Utility Methods

MethodExampleResult
max(short a, short b)Short.max((short)1, (short)2)Returns the larger value
min(short a, short b)Short.min((short)1, (short)2)Returns the smaller value
sum(short a, short b)Short.sum((short)1, (short)2)Returns the sum

Quick Summary

  • short is a 16-bit primitive integer
  • It saves memory but has a limited range
  • It has no methods, but the Short wrapper class provides useful functionality
  • int is preferred unless memory usage is critical

Further Reading


Leave a Reply

Your email address will not be published. Required fields are marked *

This site builds beginner confidence through fundamental coding concepts and regular practice. Java is the primary language, but the techniques apply across all programming languages.

Learn. Practice. Master

Categories