有人告诉我a + = b之间有区别;和a = a + b; 取决于类型差异,这可能导致其中只有一种是合法的。
有人有这样的例子吗?
基本上没有区别,但是有细微的区别。
算术赋值运算符执行隐式转换。例如
byte a = 1;
int b = 2;
a += b; // compiles
a = a + b; // doesn't compile as a byte + int = int
a = (byte) (a + b); // compiles as this is the same as +=
对于更多奇怪的例子。
int a = 5;
a += 1.5f;
// a == 6
char ch = '0'; // (char) 49
ch *= 1.1; // ch = '4';
long l = Integer.MAX_VALUE;
l += 0.0f; // i = (long ) ((long ) l + 0.0f)
// i == Integer.MAX_VALUE + 1L; WTF!?
// l is no longer Integer.MAX_VALUE due to rounding error.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句