Take the 2-minute tour ×

This may have already been answered in another post, but I just am not getting why something won't compile in my test Java app (1.7.0_01).

This compiles:

Short a = (short)17;
a = (short)2 + 1;

I know that "a + a" will result in an integer. This compiles fine:

Short a = (short)17;
int shortTest = a + a;

So why doesn't this compile?

Short a = (short)17;
a = (short)a + a;

Also, am I right to assume you can't use +=, -=, etc... on Shorts because of the conversion to integer? If it's possible to do those operations, can someone provide an example?

Edit 1
There's been some votes to close this post as it's been suggested that it's a duplicate of Primitive type 'short' - casting in Java. However, my example revolves around the Wrapper "Short" object. There are important and more complicated rules around casting Wrapper objects and that's what I think needs to be focussed on.

Also, as my original post indicates, I'm looking for the "why" behind the 3rd code block. I'm also interested to know if it's possible to use "+=", "-=", etc... on the Short Wrapper.

share|improve this question
possible duplicate of Primitive type 'short' - casting in Java –  Anthony Pegram Feb 2 '12 at 2:15
It doesn't compile because the sum of two shorts need not fit into a short. In the first case, the value are known beforehand, and so the compiler can tell that the end result will fit into the LHS. This is not so with the third case. On the other hand: Short a = (short)17; a = (short)(a + a); will work just fine. –  eternaln00b Feb 2 '12 at 2:15
@SiddharthaShankar, the sum of two integers may not fit into an integer, either. It's of a larger range, but overflows still apply. –  Anthony Pegram Feb 2 '12 at 2:16
@AnthonyPegram, Yes, I am fully aware of that. :) I was only trying to offer an explanation for the discrepancy between case 1 and 3. –  eternaln00b Feb 2 '12 at 2:20
@SiddharthaShankar The reason for the cast (with the primitive types) also probably has to do with the JVM only having opcodes to add ints, longs, floats, and doubles. So, at the JVM level, any addition of integral types smaller than long returns an int. The compiler would have to insert a cast to short if all the operands of the addition are short, which would be an unnecessary and unhelpful special case. –  millimoose Feb 2 '12 at 14:33

2 Answers 2

up vote 4 down vote accepted

Seems the correct answer was removed for some reason: (short) a + a is equivalent to ((short) a) + a, you're looking for (short)(a + a).


The 'why' behind it is operator precedence, same reason why 1 + 2 * 3 is 7 and not 9. And yes, primitives and literals are treated the same.

You can't do Short s = 1; s += 1; because it's the same as a = a + 1; where a is converted to an int and an int can't be cast to a Short. You can fix the long version like so: a = (short) (a + 1);, but there's no way to get the explicit cast in there with +=.

It's pretty annoying.

share|improve this answer
So, is "(short)2 + 1" equivalent to "((short)2) + 1"? Are integer literals treated differently than variables? Thanks for this answer, but I'm more looking for the "why" behind it...if you have thoughts on "+=", "-=" as well, that would be appreciated... –  Zack Macomber Feb 2 '12 at 13:21
Thanks Dmitri - nice clear & concise explanation. –  Zack Macomber Feb 3 '12 at 13:27

Here is a good example:

public class Example {
    public static void main(String[] args) {
        Short a = (short) 17;
        a = (short) (a + a);
share|improve this answer
Why the down votes? This is completely correct, no need to down vote a correct answer... –  theJew Feb 2 '12 at 2:27
The downvotes were because you included scatology in your answer. Please do not do this, as it detracts from the overall quality of the answer and causes you to have a bad reputation (literally). –  fireshadow52 Feb 2 '12 at 2:31
Could be anti-semetic sentiment –  Bohemian Feb 2 '12 at 2:32

Your Answer


