美文网首页
Java运算符知识点总结

Java运算符知识点总结

作者: cornprincess | 来源:发表于2020-04-08 15:31 被阅读0次

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

二元运算符

除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

相关文章

  • JAVA位运算等运算符总结

    JAVA位运算等运算符总结 一、概述 运算符是一种“功能”符号,用以通知 Java 进行相关的运算。 Java 语...

  • Java运算符知识点总结

    这里记录下运算符中重要的知识点。 二元运算符 除0 进行除法运算时需要注意, 整数除以 0 会抛出异常,但是浮点数...

  • Java运算符知识点总结

    拥有程序思维的第一步,就是要学会用计算机、或者说编写程序帮我们处理数据,而不是我们自己动手。Java语言中有很多进...

  • java

    (一) java基础面试知识点 java中==和equals和hashCode的区别==是运算符,用于比较两个变量...

  • 2.4 关系运算符

    一、学习要求 书籍参考章节: 第3.5章 知识点: 关系运算符 二、参考知识 关系运算符用于对值进行比较。java...

  • Java运算符及运算符的优先级

    Java语言中提供了很多运算符来操作变量,现总结以下七种: 赋值运算符 算术运算符 关系运算符 逻辑运算符 位运算...

  • 找工作(目录)

    知识点 JAVA基本知识 Java基本知识点总结:http://www.jianshu.com/p/6146a2d...

  • 大数据技术入门[更新中]

    总结一下目前的知识点,要掌握的知识点 一. 编程语言 1. Java ●. Java基础 面对对象,Java8新...

  • python基础(四)----运算符

    一.算术运算符(基本同Java) 二.比较运算符(基本同Java) 三.赋值运算符(基本同Java) 四.位运算符...

  • 聚焦面(考)试:那些Python与Java中,你忽略的细节

    关系运算符 最近准备考可信,顺带会查漏补缺一些Python与Java的知识点分享给大家,今天聊聊运算符优先级:关系...

网友评论

      本文标题:Java运算符知识点总结

      本文链接:https://www.haomeiwen.com/subject/roptmhtx.html