直到今天,我还以为:
i += j;
Run Code Online (Sandbox Code Playgroud)
只是一个捷径:
i = i + j;
Run Code Online (Sandbox Code Playgroud)
但是如果我们试试这个:
int i = 5;
long j = 8;
Run Code Online (Sandbox Code Playgroud)
然后i = i + j;将不会编译但i += j;将编译正常.
这是否意味着事实上i += j;是这样的捷径
i = (type of i) (i + j)?
java casting operators variable-assignment assignment-operator
我的代码如下:
char ch = t.charAt(t.length() - 1);
// result of XOR of two char is Integer.
for(int i = 0; i < s.length(); i++){
ch = ch^s.charAt(i);
ch = ch^t.charAt(i);
}
return ch;
Run Code Online (Sandbox Code Playgroud)
它抛出错误
第 6 行:错误:类型不兼容:从 int 到 char 的可能有损转换 ch = ch^s.charAt(i);
第 7 行:错误:类型不兼容:从 int 到 char 的可能有损转换 ch = ch^t.charAt(i);
2 错误
然而,当我改变
ch = ch^s.charAt(i);
ch = ch^t.charAt(i);
Run Code Online (Sandbox Code Playgroud)
到
ch ^= s.charAt(i);
ch ^= t.charAt(i);
Run Code Online (Sandbox Code Playgroud)
然后,我的代码就可以工作了。
'^=' 和 '* = ^ ' 不同吗??为什么我搜索这个关于'^='的问题,它说它们是一样的??
byte b=12;
b >>= 2; // Why is this legal? why does it automatically typecasts?
b = b >> 2; // Why is this illegal if the above is legal
Run Code Online (Sandbox Code Playgroud)