Short Class
1. What Is Short? (beginner)
short = small int (2 bytes, -32,768 to 32,767). Short = wrapper. Rare in modern code (int default!), but exams + old files/hardware use it. Same patterns as Byte/Integer!
2. Code — Range + Parse
short s = 1000; // fits!
System.out.println(s + 1); // 1001 (int math! short+int → int!)
// short t = 40000; // ERROR! (> 32767)
Short obj = Short.valueOf("1000"); // parse
System.out.println("min=" + Short.MIN_VALUE + " max=" + Short.MAX_VALUE);
// min=-32768 max=32767
short sum = (short) (s + 500); // NEED cast (int → short!)
System.out.println(sum); // 1500
3. Key Members
| Member | Use |
|---|---|
Short.MIN_VALUE / MAX_VALUE |
-32768 / 32767 |
Short.parseShort("1000") |
String → short |
obj.shortValue() |
→ primitive |
4. Traps
short + short= int (promotion — same as byte!). Cast back!parseShort("40000")→NumberFormatException.- Modern code prefers
int(faster on 32-bit CPU!);shortfor arrays/files to save memory.
5. Exam Points
- Range + promotion cast — 3 marks, occasional.
- Sibling pattern (same as Byte/Integer) — 2 marks.