Memory Usage in Java
Alex Buckley
alex.buckley at oracle.com
Fri Aug 8 17:30:26 UTC 2025
On 8/8/2025 9:47 AM, Remi Forax wrote:
> C (and Java) tricks you to think that a "short" has a single
> representation but this is not true,
> it has two representations, one on stack (local variables) and one on
> heap (fields and array elements).
...
> That's why in Java, the result of the addition of two shorts is an int,
> short s = ...
> short s2 = ...
> s + s2. // this is an int
The reason why the result of adding two shorts is an int is _because the
JLS says so_. The physical representation of short values on the stack
and on the heap -- whether in 1995 or 2025 -- is immaterial to the
strongly typed result of a numeric operator.
You could ask _why_ the JLS promotes each short to int before performing
the addition, then adds two ints producing an int. The answer is that
preferring to operate on 32-bit values made sense in 1995 and is still
acceptable today.
Alex
More information about the discuss
mailing list