Contrariwise, provide declarations for the variables x and i such that this is a legal statement:
x = x + i;
but this is not:
x += i;
At first glance, this puzzle might appear to be the same as the previous one. Rest assured, it's different. The two puzzles are opposite in terms of which statement must be legal and which must be illegal.
Solution 10: Tweedledee
Like the previous puzzle, this one depends on the details of the specification for compound assignment operators. That is where the similarity ends. Based on the previous puzzle, you might think that compound assignment operators are less restrictive than the simple assignment operator. This is generally true, but the simple assignment operator is more permissive in one area.
Compound assignment operators require both operands to be primitives, such as int, or boxed primitives, such as Integer, with one exception: The += operator allows its right-hand operand to be of any type if the variable on the left-hand side is of type String, in which case the operator performs string concatenation [JLS 15.26.2]. The simple assignment operator (=) is much less picky when it comes to allowing object reference types on the left-hand side: You can use them to your heart's content so long as the expression on the right-hand side is assignment compatible with the variable on the left [JLS 5.2].
You can exploit this difference to solve the puzzle. To perform string concatenation with the += operator, you must declare the variable on its left-hand side to be of type String. Using the simple assignment operator, the results of a string concatenation can be stored in a variable of type Object.
To make this concrete and to provide a solution to the puzzle, suppose that we precede the puzzle's two assignment expressions with these declarations:
Object x = "Buy ";
String i = "Effective Java!";
The simple assignment is legal because x + i is of type String, and String is assignment compatible with Object:
x = x + i;
The compound assignment is illegal because the left-hand side has an object reference type other than String:
x += i;
This puzzle has little in the way of a lesson for programmers. For language designers, the compound assignment operator for addition could allow the left-hand side to be of type Object if the right-hand side were of type String. This change would eliminate the counterintuitive behavior illustrated by this puzzle.
No comments:
Post a Comment
Your comments are welcome!