Community
Participate
Working Groups
javac and JBuilder's compiler allow the following statement to compile, but JDT does not: Object o = null; o += "String"; // -> The operator += is undefined for the argument type(s) Object, String // Note that 'o = o + "String"' does not result in a compiler error. It's not clear (to me, at least) from the JLS whether this should be legal or not: JLS 15.25.2 Compound Assignment Operators All compound assignment operators require both operands to be of primitive type, except for +=, which allows the right-hand operand to be of any type if the left- hand operand is of type String. A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once. ------------JLS 15.17.1 String Concatenation Operator + If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time. The result is a reference to a newly created String object that is the concatenation of the two operand strings.
On the surface, both behaviors should be consistent (o+=s and o=o+s), but interestingly enough Jikes does agree with us. Need to investigate some more.
I suspect javac got tweaked to accept this code, against the spec.
Changing our behavior to accept object += string, on the basis that object=object+string is fine. Fixed
Sincere apologies, but I just noticed in the JDK 1.4.2 release notes (http://java.sun.com/j2se/1.4.2/changes.html#javac) that Sun has changed javac to no longer allow this behavior: Bug 4642850 fixed: javac now correctly no longer allows "+=" string concatenation with an Object variable on the left-hand side. Please revert back to the original behavior. Thanks!
Good find. I had only commented the Object check, will resurrect it. Thanks for reopening. Closing