In Java, arithmetic expressions on the right hand side of the assignment evaluates to int
by default. Look at this surprising example:
short a = 1;
short b = 2;
short c = a + b; // Error!
You need to explicitly cast to short
as already mentioned in other answers, or change the method's signature to accept int
instead.
It's worth mentioning that in terms of space short
takes the same space as int
if they are local variables, class variables or even instance variables since in most systems, variables addresses are aligned, I would simply change the signature of the method to accept an int
instead and wouldn't complicate things.