Java Foundation "06" Compound assignment operation

Source: Internet
Author: User

This is the problem in the development today, although not very big, but still spent a little time to ponder.

Yes, that's good. Read the source code first

   Short value=2;   value-=2;

The source code is like the above, I write the time because of the understanding of the problem, to change this look like

   Short value=2;   value=value-2;

At this time the compiler and I on the bar, reported the second line of error. Think about it also, after the value variable and an integer operation, Java automatically promotes the result of the operation to the int type, which contradicts the short type when value is defined.

There are two suggestions for changes:

1) Change the type of value to int;

2) cast the value-2 operation result to short type;

The first code does not have an error, that means the type conversion is not a problem, that is, the compound assignment operation there is a sneaky type conversion process, but how is the conversion, the use of the amendment Opinion 1) or 2)?

Turn over the book at hand, personally understand the following:

The first piece of code is not what I understand: value-=2 and value=value-2 equate;

The exact expression should be this: Value-2 is equivalent to value= (the type of value) (value-2);

So in the compound assignment operation, the result value of his calculation is automatically cast to the left type.

Of course, the result is the same as the left-hand type without any effect.

What if the type on the left is smaller than the one that calculates the result? Will definitely lose precision, causing bugs such as:

Short value=2;//System automatic stealth type conversion value+=80000//from the high truncation, an overflow occurred, the result is not what you want System.out.println (Valuea);

To summarize:

Advantages: Complex assignment operation is convenient and simple. And very efficient, in the performance above also has the advantage;

Cons: Give you a shot in the back of your most unprotected accuracy question.

So be careful when you have a small range on the left side of the operator and the result of the right operation is a large range.

Java Foundation "06" Compound assignment operation

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.