JDK-5025685 : Autoboxing spec should specify things for extended assignment Operators too
  • Type: Enhancement
  • Component: specification
  • Sub-Component: language
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: generic
  • CPU: generic
  • Submitted: 2004-04-02
  • Updated: 2008-01-04
  • Resolved: 2008-01-04
Related Reports
Duplicate :  
Relates :  
Description
As of now Tiger-beta2 46 build:-

Operations like :-

Character ch = 95;
ch++; // is allowed
but things like
ch+=1; // not allowed leads to compile time error.

If this can be clarified at the Autoboxing spec level it will be of great help.

Comments
EVALUATION Prefix and postfix expressions are special because when the updated value is stored in the variable, the value is explicitly subject to narrowing primitive and/or boxing conversion. In ch++, the computed value is actually an int which is narrowed to char then boxed to Character for assignment in ch. Now, the familiar binary operators do nothing like that. 'ch = ch + 1' will not compile because the RHS is an int (after binary numeric promotion) and the LHS is a Character. Assignment conversion won't do a narrowing primitive followed by boxing conversion, as happened in the previous paragraph. You need to say 'ch = (char)(ch + 1)' to make it work What will not work is 'ch = (Character)(ch + 1)' because that cast is not legal by JLS 5.5! Unfortunately, that (Character) cast is what ch+=1 would need to perform. That is, allowing the compound operators to do sensible boxing/unboxing conversion would mean expanding casting conversion (5.5) too, to allow the narrowing primitive and boxing conversion pair.
23-05-2007

EVALUATION TTe spec and compiler are in agreement. We don't plan on changing this at this time. ###@###.### 2004-06-24
24-06-2004