No confusion, try the following:
short b = 32767; short c = b + 1;
What are you getting? That's right, you get:
error: incompatible types: possible lossy conversion from int to short short c = b + 1;
So what is happening on your line? System.out.println("b+1: "+(b+1)); ?
Well, b+1 too big for short , as you said correctly, but here you add an int to it, making the result also int. And 32768 is a valid int.
As others have already pointed out, if you explicitly omitted it to (short) , you received what you requested.
On the other hand, this does not work for short c = b + 1; since the declared type is short here and the actual type is int .
short c = (short) (b + 1); solves the "problem"
Lichtbringer
source share