Java运算符知识点总结

2020-04-08  本文已影响0人  cornprincess

这里记录下运算符中重要的知识点。

二元运算符

除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 执行二元操作时会先将操作数转化为同一种类型,再进行计算,转化规则为两个操作数中如果有一个是 doublefloatlong 时, 另一个数就转化为相应的doublefloatlong,如果没有,则都转化为 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 所有位进行取反, 求值过程如下:

这道题的求值过程用到了原码,反码,补码的知识,简单总结一下:

运算符优先级

优先级 分类 运算符 结合性
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 = += -= *= 、= %= &= = ^= <<= >>= >>>= 从右到左

Reference

  1. Java核心技术·卷 I(原书第10版)

  2. operators

上一篇下一篇

猜你喜欢

热点阅读