This is possible because java / android handles integer arithmetic for primitives that are less than int.
When two primitives are added to java that have a data type that is less than int, they are automatically upgraded to an integer data type. Usually, you need to convert the result to the desired data type.
The trick comes with shorthand operations, such as += , -= , etc., where the cast occurs implicitly , so the final result of the operation:
resultShort += testShort;
really resembles something like this:
resultShort = (short)((int) resultShort + (int) testShort);
If we look at the disassembled bytecode of the method:
public static int test(int a, int b){ a += b; return a; }
we see:
public static int test(int, int); Code: 0: iload_0 1: iload_1 2: iadd 3: istore_0 4: iload_0 5: ireturn
comparing this with an identical method with a replaced data type, we get:
public static short test(short, short); Code: 0: iload_0 1: iload_1 2: iadd 3: i2s 4: istore_0 5: iload_0 6: ireturn
Pay attention to the additional instruction i2s (integer to short). This is the likely culprit in performance loss. Another thing you may notice is that all instructions are based on integers denoted by the i prefix (e.g. iadd , meaning integer-add). This means that somewhere during the iload phase iload shorts moved up to integers, which could also cause performance degradation.
If you can come to terms with this, the bytecode for lengthy arithmetic is identical to integer, except that the instructions are long-specific (e.g. ladd instead of iadd ).