short
About
Definition:
shortis one of the eight primitive data types in Java used to store integer values with a smaller range compared toint. 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,768to32,767(-2^15to2^15 - 1).Default Value: The default value of
shortis0.Wrapper Class: The wrapper class for
shortisShort, located injava.lang.
Characteristics
Signed Integer Representation: The
shorttype is signed, meaning it can represent both positive and negative integers.Compact Data Type: Compared to
int, it saves memory, especially useful for large arrays of small numbers.Arithmetic Operations: Like other numeric types,
shortsupports arithmetic operations such as addition, subtraction, multiplication, and division.Promotion in Expressions: In arithmetic expressions,
shortvalues are automatically promoted tointbefore any operation. Example:short a = 10; short b = 20; int result = a + b; // Promoted to intUsage in Streams: Since Java Streams operate on
int,shortvalues must be converted or boxed to work with streams.Interoperability: Often used in file handling, network protocols, and legacy systems where compact data representation is required.
Memory Usage: Requires 16 bits (2 bytes) per value, stored in signed 2’s complement representation.
Operations with short
shortArithmetic 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)
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
Overflow Issues: Casting a larger integer to
shortcan cause data loss due to overflow.int largeValue = 70000; short overflowed = (short) largeValue; // Value wraps aroundAutomatic Promotion: Arithmetic operations promote
shorttoint, 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); // -15536Using short in Arrays
short in Arrayspublic 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
short to Stringpublic 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
short with Streamsimport 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);
}
}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