Java运算符知识点总结

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

二元运算符

除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 所有位进行取反, 求值过程如下:

  • 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 = += -= *= 、= %= &= = ^= <<= >>= >>>= 从右到左

Reference

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

  2. operators

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 进制基本概念 什么是进制?进制是一种计数的方式,数值的表示形式 常见的进制十进制、二进制、八进制、十六进制 进制书...
    极客江南阅读 6,323评论 0 11
  • java移位运算小技巧左移一位相当于乘以2的1次方,左移n位就相当于乘以2的n次方。右移一位相当于除以2的1次方,...
    java风清扬阅读 3,401评论 0 3
  • title: java基础知识tags: [java基础知识] 位运算符 java种的运算符有 “&”,“|”,“...
    thdqn阅读 1,868评论 0 0
  • 1.编译程序(1)gcc xx.c,他会默认生成一个a.out的可执行文件,在a.out所在目录,执行./a.o...
    萌面大叔2阅读 5,110评论 0 1
  • 1. 某麻醉医生说话口音较重,某次给一个大爷打硬膜外,医生问他:脚麻吗?大爷:……麻醉师觉得自己可能说话声音太小,...
    喜忧参半_8a39阅读 1,184评论 0 0

友情链接更多精彩内容