Tim Holloway wrote:. . . . Dividing a short by a short using integer division should always yield a short value.
That is what the mathematical abstraction would say, but Java has different ideas. Arithmetic is always done on types with width 32+ bits, so even if the value fits into the range of a
short, it will be perceived as an
int There is an exception: where the
short is assigned from a compile‑time constant of the appropriate magnitude, in which case an implicit cast is applied. Otherwise the code will fail to compile.
. . . when you exceed the capacity of the largest intermediate data type what the work can be widened to, it will throw an arithmetic exception. . . .
Before Java8, all overflow errors occurred silently and didn't throw exceptions. Whether that was the right decision or wrong……well, I can see arguments on both sides. There are
xyzExact() methods, introduced in Java8, which
test for overflow and throw an exception if it happens.