Differences between X = x + 1 and x + = 1 in Java

Source: Internet
Author: User

Today, I learned a little bit about the Java compound assignment operation (+ =). record it and thank you for your explanation!

First, let's look at the following two linesCode:

 
Short S1 = 1; S1 = S1 + 1;

For those with a little java experience, this is a very simple problem. They will certainly say that this is not possible through compilation.

First, because the short type is 16 bits, and the int type is 32 bits(S1 + 1) during operation, S1 is automatically raised to 32 bits, and then added to I,

The result is 32 bits.S1 = S1 + 1; an error is inevitable,This will result in the loss of 2 bytes of precision, which is not allowed. However, you can execute strong conversion:

S1 = (short) (S1 + 1.

Now let's look at the following two lines of code:

 
Short S1 = 1; S1 + = 1;

ManyProgramMembers will think that the expression (S1 + = 1) Here is just a shorthand for the above expression (S1 = S1 + 1), at least I thought so before. However, this is not very accurate.

Both expressions are called value assignment expressions. The statement at the beginning uses the simple value assignment operator (=), and here this statement uses the compound value assignment operation (+ = ).

In the Java language specification, the compound value assignment E1 op = e2 is equivalent to a simple value assignment e1 = (t) (E1) OP (E2) where T is the data type of E1, OP is the operator.

This type conversion is either a constant conversion or a narrow conversion.

In other words, compound value assignment expressions automatically convert the results of the calculations they perform to the type of the variable on the left. If the result type is the same as the type of the variable, this transformation will not have any impact. However, if the result type is wider than the variable type, the composite value assignment operator will quietly perform a narrow primitive type conversion.

But can we enable the JVM to automatically narrow the conversion for us? Let's take a look at the following question:

 
Short S1 = short. max_value; // 32767s1 + = 1; system. Out. println (S1 );

You may think the result is 32768, but when you run it, you will find that it will print:-32768. At this time, you may suddenly realize that we have lost precision, so we are using compound

Pay special attention to the value assignment operation (+ =). In this case, it will narrow the original type conversion (even if precision is lost ).

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.