The difference between the short type + operation and the + + operation doesn't even have a correct answer.

Source: Internet
Author: User

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.

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.