这里记录下运算符中重要的知识点。
二元运算符
除0
进行除法运算时需要注意, 整数除以 0 会抛出异常,但是浮点数除以 0 会得到无穷大或者 NaN。
<pre mdtype="fences" cid="n7" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">System.out.println(0.0 / 0); // NaN
System.out.println(1.0 / 0); // Infinity
System.out.println(1 / 0); // Exception in thread "main" java.lang.ArithmeticException: / by zero</pre>
隐式类型转换
<pre mdtype="fences" cid="n20" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">short s1 = 1;
s1 = s1 + 1; // error</pre>
Java 执行二元操作时会先将操作数转化为同一种类型,再进行计算,转化规则为两个操作数中如果有一个是 double
或 float
或 long
时, 另一个数就转化为相应的double
或 float
或 long
,如果没有,则都转化为 int
类型再进行计算,上述 s1 + 1
先将 s1 转化为 int 类型(隐式转换),然后执行 + 运算,操作得出的结果也是 int 类型, 不能赋值给 short 类型,因此会出错。
<pre mdtype="fences" cid="n24" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">s1 += 1;
s1++;</pre>
但是上述语句是有效的,因为其等同于 s1 = (short) (s1 + 1);
位运算
<pre mdtype="fences" cid="n54" lang="java" class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">~4 = -5;</pre>
上述代码对 4 进行~
运算,对 4 所有位进行取反, 求值过程如下:
4 (0000 0000 0000 0000 0000 0000 0000 0100)按位取反:1111 1111 1111 1111 1111 1111 1111 1011(补码)
求反码(补码 - 1):1111 1111 1111 1111 1111 1111 1111 1010(反码)
求原码(最高位不变,其他位取反):1000 0000 0000 0000 0000 0000 0000 0101(-5)
这道题的求值过程用到了原码,反码,补码的知识,简单总结一下:
带符号整数有原码、反码、补码等几种编码方式。原码即直接将真值转换為其相应的二进制形式,而反码和补码是对原码进行某种转换编码方式。
计算机运算是都是通过补码进行运算,原因是为了只使用加法运算而不使用减法运算
求位运算结果需要将最终的补码转换为原码,再通过原码求其真值
整数的原码,反码,补码都相同
复数的反码,补码需要根据原码进行转换计算
运算符优先级
优先级 | 分类 | 运算符 | 结合性 | ||
---|---|---|---|---|---|
1 | method call | [] . ()(方法调用) | 从左到右 | ||
2 | postfix | exper++ exper-- | 从右到左 | ||
3 | unary | ++exper --exper +exper -exper ~ ! ()(强制类型转换) new | 从右到左 | ||
4 | multiplicative | * / % | 从左到右 | ||
5 | additive | + - | 从左到右 | ||
6 | shift | << >> >>> | 从左到右 | ||
7 | relational | < > <= >= instanceof | 从左到右 | ||
8 | equality | == != | 从左到右 | ||
9 | bitwise AND | & | 从左到右 | ||
10 | bitwise exclusive OR | ^ | 从左到右 | ||
11 | bitwise inclusive OR | 从左到右 | |||
12 | logical AND | && | 从左到右 | ||
13 | logical OR | 从左到右 | |||
14 | ternary | ? : | 从右到左 | ||
15 | assignment | = += -= *= 、= %= &= | = ^= <<= >>= >>>= | 从右到左 |