Today again in the Wei Chen forum to see people ask this question:
Short tmp = 0;
Why TMP = TMP +1; error but TMP + + is correct.
Google and Yahoo do not search for a correct answer. All say Java programmers don't need to care about the bottom, really?
In S+1, the + operation has two operands, the minimum type of the JVM instruction + operation is int, and the other byte, short, char are operated by Iload,iadd,
Then use I2B,I2S,I2C to convert back, because the + operation in the middle of two, so if not manually converted, the compiler does not know you want to
Converts to the first operand type or the second operand type.
s++, the equivalent of the increment operation on S, the purpose operand is s, its type is determined to be short, so after Iadd, the compiler automatically
Call I2S to convert int to short.
You do not think that 1 is the default int, so the tmp+1 defaults to the maximum type. Try the following operation to know:
Short a = 0;
Short B = 0;
Short C = A +b, two short does not exist with the maximum type of operation is converted to int transport forget it?
In fact it still does not work, because short also can only use iload,iadd instruction operation, so short C = A+b also must be manually converted.