相关疑难解决方法(0)

为什么Java的+ =, - =,*=,/ =复合赋值运算符需要转换?

直到今天,我还以为:

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

3547
推荐指数
11
解决办法
28万
查看次数

java中的'^='运算符是什么

我的代码如下:

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)

然后,我的代码就可以工作了。

'^=' 和 '* = ^ ' 不同吗??为什么我搜索这个关于'^='的问题,它说它们是一样的??

什么是“^=”运算符?

java bitwise-xor

3
推荐指数
1
解决办法
95
查看次数

使用速记运算符进行类型转换

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)

java

2
推荐指数
1
解决办法
259
查看次数