Yes, floats and doubles are both stored in the IEEE754 format, so they have the same precision problems, but worse for floats. I presume promotion would entail filling the mantissa with 0s, so the promoted value will not be the same as the double. I don't know whether the Java® Language Specification (=JLS) says anything more specific.
Jeanne Boyarsky wrote:. . . Not sure if floats are, but I imagine the same . . .
That link does indeed show the standard way to print the runtime type of a reference variable. It even works for arrays, but the output looks strange if you aren't used to it. You can't do the same for primitives becaue they don't have a getClass() method. They don't have any methods in fact. Junilu has shown that the compiler can predict what the runtime type of s1 + s2 is, but not the individual variables:-
Sam Peterson wrote:. . . I wasn't the first one to think about this idea. . .
You cannot, as far as I know, see that during that calculation, both shorts are promoted to ints, and revert to being shorts, their declared type, when the arithmetic has finished.
The same code as Junilu's wrote:jshell> WhatIs.typeOf(s1 + s2)
$15 ==> "Primitive: int"
So it can only be assigned to an int or a long, or must be explicitly cast.
The type of an additive expression on numeric operands is the promoted type of its operands.