Puzzle 9: Half a catty

Source: Internet
Author: User

Now it's your turn to write some code. The good news is that you only need to write two lines of code for the puzzle and two lines of code for the next puzzle. What's the problem? Let's give a declaration of the variables X and I. It must be a legal statement:

x += i;

However, it is not:

x = x + i;

Many Programmers think that the first expression (x + = I) in this question is just a shorthand for the second expression (x = x + I. However, this is not very accurate. Both expressions are called value assignment expressions. The second statement uses the simple value assignment operator (=), while the first statement uses the compound value assignment operator. (The compound assignment operators include + =,-=, * =,/=, % =, <=, >>=, >>>=, <=, ^ =, and | =) in the Java language specification, the compound assignment of E1 op = e2 is equivalent to a simple assignment of E1 = (t) (E1) OP (E2), where T is the type of E1, unless E1 is calculated only once.

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. Therefore, we have a good reason to explain why a compilation error may occur when trying to execute an equivalent simple assignment.

To be specific and provide a solution to the puzzle, let's assume that we have the following statements before the two values of the puzzle:

short x = 0;int i = 123456;

Composite value assignment compilation will not produce any errors:

X + = I; // contains a hidden transformation!

You may expect the value of X to be 123,456 after the statement is executed, but not so l. Its value is-7,616. The Int value 123456 is too large for short. The automatic transformation quietly cut off the two high int values. This may not be what you want.

The corresponding simple value assignment is invalid because it tries to assign the int value to the short variable and requires an explicit Transformation:

X = x + I; // do not compile -- "precision may be lost"

This should be obvious, and the compound assignment expression may be dangerous. To avoid this unpleasant raid, do not apply the composite value assignment operator to byte, short, or char type variables. When you apply a composite value assignment operator to an int type variable, make sure that the right side of the expression is not of the long, float, or double type. When you apply a composite value assignment operator to a float variable, make sure that the right side of the expression is not a double type. These rules are sufficient to prevent the compiler from generating a dangerous narrow transformation.

In short, the compound assignment operator will quietly generate a transformation. If the calculation result type is wider than the variable type, the resulting transformation is a dangerous narrow transformation. Such a transformation may quietly discard the precision or quantity value. For Language designers, it may be an error to make the composite value assignment operator produce an invisible transformation. For variable types in composite values that are narrower than calculation results, maybe it should be illegal.

Puzzle 9: Half a catty

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.