There is no correct answer to the difference between a short type + operation and a ++ operation.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.