Home Java Integer Methods shortValue() Java Integer shortValue() Method Definition In Java, boxed.shortValue() converts an Integer to a primitive short, a 16-bit number from -32768 to 32767. The method belongs to Number, so it exists since Java 1.1.
If the value is outside that range, nothing is thrown. Java keeps only the low 16 bits , so the result can look very different from the original number.
An instance method of java.lang.Integer (since Java 1.1): call it on an Integer object. Integer lives in java.lang, so no import is needed.
Syntax Call it on an Integer. It takes no arguments.
Integer a = 100;
short x = a.shortValue(); // 100
Integer b = 40000;
short y = b.shortValue(); // -25536 (wrapped!) Call What it does boxed.shortValue()The low 16 bits as a short. (short) iThe same narrowing for a primitive int. Short.MIN_VALUE / Short.MAX_VALUEThe limits of short: -32768 and 32767. i >= Short.MIN_VALUE && i <= Short.MAX_VALUEA check that the value fits before you narrow it.
shortValue() never throws. If the value does not fit, the result silently wraps around.
Library declarations // java.lang.Integer
public short shortValue()
// Inherited contract from java.lang.Number Parameters Parameter Description None shortValue() takes no parameters.
Return Value Return Value Description shortThe low 16 bits of the value, read as a signed number from -32768 to 32767.
Important
Because the conversion discards the upper 16 bits, 32768 becomes -32768, 40000 becomes -25536, and 65536 becomes 0. Check the range yourself with Short.MIN_VALUE and Short.MAX_VALUE before you narrow a value you do not control. Narrowing is almost never needed for arithmetic, because Java promotes short to int before it calculates.
Compare with Similar Methods Related conversions:
Java Examples 1. In range and out of range 2. Every 65536 3. Check before narrowing Example 1 — Narrow Numbers to short Convert a mix of small and large values and see which ones survive.
public class IntegerShortValueDemo {
public static void main(String[] args) {
Integer[] numbers = {100, -100, 32767, 32768, 40000, 70000, -40000};
for (Integer number : numbers) {
System.out.println(number + " -> " + number.shortValue());
}
}
} 100 -> 100
-100 -> -100
32767 -> 32767
32768 -> -32768
40000 -> -25536
70000 -> 4464
-40000 -> 25536 How It Works In range stays the same. 100, -100, and 32767 fit in a short, so they come back unchanged.One past the top. 32768 needs a 17th bit, which is dropped, so the value wraps to -32768.40000 and 70000. Subtracting 65536 gives -25536 for 40000, and 70000 leaves 4464 after the wrap.Negative values wrap the other way. -40000 is below -32768, so adding 65536 gives 25536.Example 2 — See the Pattern Behind the Wrap Print values around multiples of 65536 to see that short repeats every 16 bits.
public class IntegerShortValueWrap {
public static void main(String[] args) {
Integer[] numbers = {65535, 65536, 65537, 98304, 131072};
for (Integer number : numbers) {
short s = number.shortValue();
System.out.println(number + " -> " + s + ", low 16 bits: "
+ Integer.toHexString(number & 0xFFFF));
}
}
} 65535 -> -1, low 16 bits: ffff
65536 -> 0, low 16 bits: 0
65537 -> 1, low 16 bits: 1
98304 -> -32768, low 16 bits: 8000
131072 -> 0, low 16 bits: 0 How It Works Only 16 bits survive. The hex column shows the low 16 bits that shortValue() keeps.65535 is all ones. Its low 16 bits are ffff, which a signed short reads as -1.65536 is a clean wrap. The low 16 bits are all zero, so the result is 0; 65537 gives 1.Halfway points. 98304 keeps 8000, the pattern for -32768, while 131072 is two full wraps and gives 0.Example 3 — Check the Range Before You Narrow Write a helper that narrows only when the value fits, and reports a problem otherwise.
public class IntegerShortValueSafe {
static String toShort(Integer value) {
if (value >= Short.MIN_VALUE && value <= Short.MAX_VALUE) {
return "short " + value.shortValue();
}
return "does not fit";
}
public static void main(String[] args) {
Integer[] numbers = {500, 32767, 32768, -32768, -32769};
for (Integer number : numbers) {
System.out.println(number + ": " + toShort(number));
}
System.out.println("limits: " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
}
} 500: short 500
32767: short 32767
32768: does not fit
-32768: short -32768
-32769: does not fit
limits: -32768 to 32767 How It Works Test first. The helper compares the number with Short.MIN_VALUE and Short.MAX_VALUE before it narrows.Edges that fit. 32767 and -32768 are exactly the limits, so both are accepted.One step outside. 32768 and -32769 are rejected instead of wrapping to a surprising value.Constants show the limits. Short.MIN_VALUE and Short.MAX_VALUE are -32768 and 32767.Frequently Asked Questions What does Integer.shortValue() do? It returns the low 16 bits of the Integer as a primitive short, which holds numbers from -32768 to 32767.
What happens if the Integer is too big for a short? No exception is thrown. The upper bits are dropped, so 40000 becomes -25536.
How do I check that an int fits in a short? Compare it with Short.MIN_VALUE and Short.MAX_VALUE before narrowing, as in value >= Short.MIN_VALUE && value <= Short.MAX_VALUE.
What is the difference between shortValue() and a (short) cast? For an Integer they give the same result. The cast works on a primitive int; shortValue() works on an Integer object.
Do I need short at all? Rarely. short saves memory in large arrays, but Java promotes it to int in arithmetic, so int is the usual choice.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
7 people found this page helpful
Helpful Share Copy link Suggestion