They specified 1.5 in the title. So, they CAN use the "+" operator with an Integer object. It's called "autoboxing" (in this case unboxing actually) which is a new feature of the Java language in 1.5. The reason you can't do the second operation is because the left-hand side of an assignment has to be a variable. In your case, it's not. It's an expression (the result of casting i to an Integer).
James Carman, President<br />Carman Consulting, Inc.
Also note that += is not the unary + operator. The latter is similar to the unary - operator:
Joined: Jul 18, 2004
I tryed some test cases, and when not casting on the left it works fine. Why can't expressions such as that be on the left side? Doesn't casting effectivly 'replace' what was there with the casted object?
The problem is your casting parenthesis. Try this:
( ( Integer ) i ) += 1;
Joined: Aug 07, 2003
Originally posted by Dan Bizman: Try this: ( ( Integer ) i ) += 1;
I tried it using JDK 1.5 for kicks, and it failed. As James said, you need an assinable variable on the left side of an assignment operator (called an lvalue). Casting a variable evalutes to the value the variable holds cast to the specified type which is not assignable (called an rvalue).