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

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 221,695评论 6 515
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 94,569评论 3 399
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 168,130评论 0 360
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 59,648评论 1 297
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,655评论 6 397
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 52,268评论 1 309
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,835评论 3 421
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,740评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 46,286评论 1 318
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,375评论 3 340
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,505评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 36,185评论 5 350
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,873评论 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,357评论 0 24
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,466评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,921评论 3 376
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,515评论 2 359

推荐阅读更多精彩内容

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