13

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.

Community
  • 1
  • 1
Zack Macomber
  • 6,682
  • 14
  • 57
  • 104
  • 1
    possible duplicate of [Primitive type 'short' - casting in Java](http://stackoverflow.com/questions/477750/primitive-type-short-casting-in-java) – Anthony Pegram Feb 02 '12 at 02: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 02 '12 at 02: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 02 '12 at 02: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 02 '12 at 02:20
  • So, does "(short)a + a" only cast the first a to Short and not the entire expression? – Zack Macomber Feb 02 '12 at 02:21
  • @ZackMacomber Yes, the cast only applies to the first "short". :) – eternaln00b Feb 02 '12 at 02:26
  • Ugh - this was a stupid question - been studying too long and thinking about the internals of Java too much to not see the obvious...the last statement adds a short to an int which results in an int which the Short can't accept without an explicit cast - oops - voting to close... – Zack Macomber Feb 02 '12 at 02:27
  • Actually, maybe I won't close...why does the cast on the 1st example work fine and the cast on the 3rd not work? – Zack Macomber Feb 02 '12 at 02:34
  • @ZackMacomber My guess is because in the third example, you're trying to add a simple variable value to an Object value. Though the two have identical names, they do not have identical properties (but similar). The first example works because both `2` and `1` are universal; they can be used anywhere by themselves. – fireshadow52 Feb 02 '12 at 02:38
  • @AnthonyPegram - I've looked a couple of times at the "Primitive type 'short' - casting in Java" post and I've just realized that post is dealing with the primitive 'short' - my post is slightly different as it deals with the wrapper class 'Short' which is not a primitive type - I'm understanding this more and more but it's good to note that there's more complexity involved here with the casting involved as a wrapper object is used... – Zack Macomber Feb 02 '12 at 13:45
  • 1
    @SiddharthaShankar The reason for the cast (with the primitive types) also probably has to do with the JVM only having opcodes to add `int`s, `long`s, `float`s, and `double`s. 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 02 '12 at 14:33

1 Answers1

12

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).

Edit

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.

Dmitri
  • 8,999
  • 5
  • 36
  • 43
  • 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 02 '12 at 13:21