pol*_*nts 8 java autoboxing compiler-errors implicit-cast compound-assignment
由于复合赋值和递增/递减运算符中的隐式转换,以下编译:
byte b = 0;
++b; b++; --b; b--;
b += b -= b *= b /= b %= b;
b <<= b >>= b >>>= b;
b |= b &= b ^= b;
Run Code Online (Sandbox Code Playgroud)
并且由于自动装箱和自动拆箱,以下还编译:
Integer ii = 0;
++ii; ii++; --ii; ii--;
ii += ii -= ii *= ii /= ii %= ii;
ii <<= ii >>= ii >>>= ii;
ii |= ii &= ii ^= ii;
Run Code Online (Sandbox Code Playgroud)
然而,以下代码段中的最后一行给出了编译时错误:
Byte bb = 0;
++bb; bb++; --bb; bb--; // ... okay so far!
bb += bb; // DOESN'T COMPILE!!!
// "The operator += is undefined for the argument type(s) Byte, byte"
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮我弄清楚这里发生了什么?该byte b版本编译就好了,所以不应该Byte bb只是跟风而做相应的装箱和拆箱的必要,以适应?
那么,有没有一种方法,使复合赋值运算符与工作Byte,Character以及Short在左侧,或者他们只是非法的(!!!)这些类型的?
From type boolean to type Boolean
From type byte to type Byte
From type char to type Character
From type short to type Short
From type int to type Integer
From type long to type Long
From type float to type Float
From type double to type Double
Run Code Online (Sandbox Code Playgroud)
注意没有int to Byte.当你这样做时,bb + bb它被转换为int + int,而不是盒装回Byte.对于byte版本,int + int将byte直接转换回(缩小原始转换,第5.1.3节),因此允许使用.
| 归档时间: |
|
| 查看次数: |
317 次 |
| 最近记录: |