Java Reference
In-Depth Information
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.
< Day Day Up >
Search WWH ::
Custom Search