JDK-6686855 : Section 15.26.2 iin JLS 3 incorrectly allows += to be applied to Object, String.
  • Type: Bug
  • Component: specification
  • Sub-Component: language
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2008-04-10
  • Updated: 2011-02-16
  • Resolved: 2008-04-10
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :


A DESCRIPTION OF THE PROBLEM :
JLS 2: 15.26.2 begins:

"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. Note that the implied cast to type T may be either an identity conversion (��5.1.1) or a narrowing primitive conversion"


while JLS 3: 15.26.2 begins with only:

"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"


In addition to the intended purpose of the change (allowing primitive wrappers to be used), this would also allow:

Object o = new Object();
o += "test";

since:

Object o = new Object();
o = (Object)((o) + ("test"));

is legal. The former is not accepted by javac.



REPRODUCIBILITY :
This bug can be reproduced always.