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
Shortwrapper class. They cannot be called directly on a primitiveshortvalue and will cause a compile-time error if you try.
Constants
| Constant | Example | Result |
| MAX_VALUE | Short.MAX_VALUE | Returns 32,767 |
| MIN_VALUE | Short.MIN_VALUE | Returns –32,768 |
| SIZE | Short.SIZE | Returns 16 (16 bits) |
| BYTES | Short.BYTES | Returns 2 (2 bytes) |
Conversion Methods
parseShort(String)
short value = Short.parseShort("123");
- Converts a numeric string into a
short - Throws a
NumberFormatExceptionif the string is not numeric (e.g."hello")
toString(short)
String text = Short.toString((short) 42);
- Converts a
shortvalue into aString - 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:
0if 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
trueif values are equal - Returns
falseotherwise
Utility Methods
| Method | Example | Result |
| 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
shortis a 16-bit primitive integer- It saves memory but has a limited range
- It has no methods, but the
Shortwrapper class provides useful functionality intis preferred unless memory usage is critical
Leave a Reply