Today, I again saw someone asking this question on the weitqin Forum:
Short TMP = 0;
Why TMP = TMP + 1; error but TMP ++; is correct.
Google and Yahoo cannot find a correct answer.ProgramMembers do not need to care about the underlying layer. Is it true?
In S + 1, The + operation has two operands. The minimum Type of the + operation in the JVM command is int, and other bytes, short, and Char are operated by iload and iadd,
Then use i2b, I2S, and I2C to convert back, because the + operation is in the middle of two numbers, so if you do not manually convert, the compiler does not know that you want
Convert to the first operand type or the second operand type.
In S ++, it is equivalent to the increment operation on S. The destination operand is s, and its type is determined to be short. Therefore, after iadd, the compiler automatically
Call I2S to convert int to short.
Do not think that 1 is int by default, so TMP + 1 is calculated based on the maximum type by default. Just try the following operations:
Short a = 0;
Short B = 0;
Short c = A + B; the two short operations are not converted to the int type by the maximum type operation, right?
In fact, it still does not work, because short can only use iload, iadd command operations, so short c = a + B must also be manually converted.