You are simply overflowing the maximum value of a short :

short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

What happens when there is such overflow is equivalent to this algorithm :

/** Returns an integer which is equal to the short obtained by the ((short) n) conversion */
public static int int2short(int n) {
    int sign      = n > 0 ? 1 : -1;
    int increment = sign * (Short.MAX_VALUE - Short.MIN_VALUE + 1);
    for ( ; n > Short.MAX_VALUE || n < Short.MIN_VALUE ; n -= increment);
    return n;
}
Answer from Dici on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
It has a minimum value of -128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Short.html
Short (Java Platform SE 8 )
October 20, 2025 - A constant holding the minimum value a short can have, -215. ... A constant holding the maximum value a short can have, 215-1.
Top answer
1 of 4
4

You are simply overflowing the maximum value of a short :

short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.

What happens when there is such overflow is equivalent to this algorithm :

/** Returns an integer which is equal to the short obtained by the ((short) n) conversion */
public static int int2short(int n) {
    int sign      = n > 0 ? 1 : -1;
    int increment = sign * (Short.MAX_VALUE - Short.MIN_VALUE + 1);
    for ( ; n > Short.MAX_VALUE || n < Short.MIN_VALUE ; n -= increment);
    return n;
}
2 of 4
3

Good question! It made me think about things I haven't thought about in a long while and I had to brush up on a couple of concepts. Thanks for helping me knock the rust off my brain.

For me this type of question is best visualized in binary (for reasons that will quickly become apparent):

Your original number (forgive the leading zeroes; I like groups of 4):

0001 1110 0010 0100 0000

A short, however, is a 16-bit signed two's complement integer according to the Java Language Specification (JLS) section 4.2. Assigning the integer value 123456 to a short is known as a "narrowing primitive conversion" which is covered in JLS 5.1.3. Specifically, a "narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits, where n is the number of bits used to represent type T."

Discarding all but the lowest 16 bits leaves us with:

1110 0010 0100 0000

In an unsigned integer this value is 57,290, however the short integer is a signed two's complement integer. The 1 in the leftmost digit indicates a negative number; to get the value of the number you must invert the bits and add 1:

Original:

1110 0010 0100 0000

Invert the bits:

0001 1101 1011 1111

Add 1:

0001 1101 1100 0000

Convert that to decimal and add the negative sign to get -7,616.

Thanks again for asking the question. It's okay to not know something so keep asking and learning. I had fun answering...I like diving into the JLS, crazy, I know!

🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Short.html
Short (Java Platform SE 7 )
A constant holding the minimum value a short can have, -215. ... A constant holding the maximum value a short can have, 215-1.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-guava-shorts-max-method-with-examples
Java Guava | Shorts.max() method with Examples - GeeksforGeeks
July 11, 2025 - Return Value:This method returns a short value that is the maximum value in the specified array. Exceptions:The method throws IllegalArgumentException if the array is empty. Below programs illustrate the use of the above method: Example-1 : ... // Java code to show implementation of // Guava's Shorts.max() method import com.google.common.primitives.Shorts; import java.util.Arrays; class GFG { // Driver's code public static void main(String[] args) { // Creating a short array short[] arr = { 2, 4, 6, 10, 0, -5, 15, 7 }; // Using Shorts.max() method to get the // maximum value present in the array System.out.println("Maximum value is : " + Shorts.max(arr)); } }
🌐
Tutorialspoint
tutorialspoint.com › java › lang › java_lang_short.htm
Java - Short class with Examples
Following is the declaration for java.lang.Short class − · public final class Short extends Number implements Comparable<Short> Following are the fields for java.lang.Short class − · static short MAX_VALUE − This is the constant holding the maximum value a short can have, 215-1.
Find elsewhere
🌐
Reddit
reddit.com › r/learnprogramming › short variable on java
r/learnprogramming on Reddit: Short variable on Java
October 23, 2022 -

Hello everyone, started learning how to code by myself a few weeks ago and I'm trying to understand something and can't find the answer nowhere on google. When I try to creat a HashMap with the short variable, why do I need to write "<Short>" before the number? It doesn't happen with Integer, String or Double (have not tested the other variables yet).

public class IDandPASS {

	HashMap<String,Short> logininfo = new HashMap<String,Short>();

	IDandPASS(){

	**logininfo.put("OP",452);  (WRONG)**

	**logininfo.put("OP",<Short>452); (CORRECT)**
Top answer
1 of 3
6
Because a number without suffix is an int by default, a decimal number without a suffix is a double and so on. There are suffixes for long (L/l) and float (F/f), but not for short. This alone is reason enough to just avoid short unless you really need it. "Saving" 2 bytes vs int is irrelevant in most cases. With wrapper types (Short vs short, Integer vs int) where they have some overhead considering size it becomes even less relevant saving 2 bytes I'd just switch to Integer there.
2 of 3
3
I assume you mean (Short), as is a syntax error here. Java integer literals are 32 bits in size (i.e. an int), or 64 bits if you have an L at the end (i.e. a long) Short literal values (16 bits) do not exist in Java, there is no syntax for them, because you rarely ever need to use them. Your computer usually operates fastest when reading and writing 32 bits. The problem here is you are storing said short in a collection that uses generic types. This means any primitive (short) has to be boxed within a reference type (Short). Java will complain if you try to assign a primitive int literal in a Short reference type due to how the boxing is handled by the compiler and because it will try to warn you that you are truncating the most significant 16 bits off the literal, meaning you will end up needing to explicitly cast the value to suppress this. If you pass in a short variable, it won't need you to do this. In your case, using an Integer in the collection type makes more sense as it avoids these ugly casts everywhere. The memory usage difference is irrelevant since primitives usually get allocated on 4 byte (32 bit) boundaries anyway due to performance benefits in the JVM. The only time you really ever need to use short is if you have found from performance testing that it is actually beneficial over int, or if you have large arrays of integer values that can be stored in 16 bits (since the 4 byte boundary rule will probably not be enforced for arrays). Map map = new HashMap<>(); map.put("user", 456); You'll find a similar thing occurs if you were to use Byte here. Bytes have more usages though since they are the defacto representation for binary data (from stuff like sockets, native memory, shared memory, files, and are used in a lot of JVM internals as well). I believe if you used Long here, you'd see a similar issue. You'd work around that by adding an L on the end of the number literal though. Map map = new HashMap<>(); map.put("user", 456L); TL;DR it is due to quirks in how the type system works with implicit integer width conversions. Other languages may handle this better than Java but it is there to make you think about how you are using the values.
🌐
TutorialsPoint
tutorialspoint.com › display-the-minimum-and-maximum-value-of-primitive-data-types-in-java
Display the minimum and maximum value of primitive data types in Java
Every data type in Java has a minimum as well as maximum range, for example, for Float. Min = 1.4E-45 Max = 3.4028235E38 Let’s say for Float, if the value extends the maximum range displayed above,
🌐
Studyopedia
studyopedia.com › home › java program to display the minimum and maximum value of primitive data types
Java Program to display the minimum and maximum value of primitive data types - Studyopedia
August 18, 2020 - byte: An 8-bit signed two’s complement integer Minimum Value: -128 Maximum Value: 127 · short: A 16-bit signed two’s complement integer Minimum Value: -32768 Maximum Value: 32767
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › Short.html
Short (Java Platform SE 6)
A constant holding the minimum value a short can have, -215. ... A constant holding the maximum value a short can have, 215-1.
🌐
LabEx
labex.io › tutorials › java-how-to-check-if-an-integer-is-within-short-range-in-java-560007
How to Check If an Integer Is Within Short Range in Java | LabEx
Always be mindful of the data type's ... understanding its 16-bit signed two's complement nature and its specific range from -32,768 to 32,767....
🌐
W3Resource
w3resource.com › java-tutorial › java-premitive-data-type.php
Java Primitive data type - w3resource
It has a minimum value of -32,768 and a maximum value of 32,767 (inclusive). As with byte, the same guidelines apply: you can use a short to save memory in large arrays, in situations where the memory savings actually matters.
🌐
Codemia
codemia.io › knowledge-hub › path › setting_short_value_java
Setting Short Value Java
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Quora
quora.com › What-are-byte-short-and-int-numbers-for-in-Java-Why-should-you-limit-your-numbers-to-a-certain-value-instead-to-the-max-possible
What are byte, short and int numbers for in Java? Why should you limit your numbers to a certain value instead to the max possible? - Quora
Answer (1 of 6): If you are buying an automobile will you always buy the largest vehicle available? For instance, would you buy one of these: or possibly one of these: Bigger definitely has its uses, but bigger is not always better. While either of these examples could be used to carry a loaf o...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-get-size-minimum-and-maximum-value-of-data-types-in-java
How to Get Size, Minimum, and Maximum Value of Data Types in Java? - GeeksforGeeks
Value 1 Byte 8 -128 127 2 Short 16 -32768 32767 3 Integer 32 -2147483648 2147483647 4 Float 32 1.4E-45 3.4028235E38 5 Long 64 -9223372036854775808 9223372036854775807 6 Double 64 4.9E-324 1.7976931348623157E308 7 Character 16
Published   March 28, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-max_value-and-integer-min_value-in-java-with-examples
Integer.MAX_VALUE and Integer.MIN_VALUE in Java with Examples - GeeksforGeeks
July 12, 2025 - Example 2: Trying to initialize a variable value Integer.MAX_VALUE + 1 ... // Java program to show what happens when // a value greater than Integer.MAX_VALUE // is stored in an int variable class GFG { // Driver code public static void ...