在Java中给出了这个:
String a = "str";
CharSequence b = "charseq";
你可以写
b = b + a;
但不能写(给出编译器错误)
b += a;
错误是
incompatible types
found : java.lang.CharSequence
required: java.lang.String
现在在 JLS 第二版中,这可以通过15.26.2 复合赋值运算符中的这一行来解释:
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.
但在 JLS 第三版中,这条评论消失了,关于复合运算符的唯一说法是在15.26.2 Compound Assignment Operators:
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.
这似乎不起作用(见上文)。
所以我的问题是 - javac 和 JLS 之间究竟是什么关系,这个特定示例是 javac 中的错误还是 JLS 中的错误?