I'm currently working with short
, but every operations between shorts gives me int
. I'm aware that Java doesn't provide short literals, so cast is required when declaring/assigning a new short variable.
But why standard operations (+ - / *) give an int
as a result?
Is it because of Java auto-boxing mechanism?
Exemple (JDK 1.8):
private short prepareValue(short value)
{
// here: no literal, so cast, but cast is not required for test?
short sign = (value < 0) ? (short)-1 : (short)1;
// here: short*short=int, so casting the result
short absValue = (short)(value * sign);
// here: the function returns a short, but same problem as before
value = (short)(myFunctionOnlyOnPositive(absShort) * sign);
// ... more things
return value;
}
I took a look into Conversions and Contexts, but I didn't find any help on it. Even the documentation on Short isn't helping (I must be dumb or something else at this point). So can someone explain to me why does Java assume short * short = int
?