I have encountered an interesting problem when Java programming. I do not recall that I had such a problem previously. I declared two variables. Type of x
is byte
and y
is short
. Summation of x
and 5
cannot be assigned to y
.
byte x = 3;
short y = 103;
y = x + 5;
NetBeans gives possible lossy conversion from int to short
error. However, there is no problem with y = 5
. I also tried y = x + (short)5
. But this doesn't work either. Only y = (short)(x + 5)
works. Moreover, y = x + x
doesn't work. It is weird. Does Java compiler automatically convert result of any calculation to int
?