## Java编程那些事儿28—运算符优先级
郑州游戏学院 陈跃峰
出自:[http://blog.csdn.net/mailbomb](http://blog.csdn.net/mailbomb)
**4.7 运算符优先级**
在实际的开发中,可能在一个运算式中出现多个运算符,那么计算时,就按照优先级级别的高低进行计算,级别高的运算符先运算,级别低的运算符后计算,具体运算符的优先级见下表:
运算符优先级表
<table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 69.2pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" border="1" cellspacing="0" cellpadding="0"><tbody><tr><td style="padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; padding-top: 0cm; border: black 1pt solid;" width="57" valign="top"><div>优先级</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: black 1pt solid; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>运算符</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: black 1pt solid; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>结合性</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>1</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>() [] .</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左到右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>2</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>! +(正) -(负) ~ ++ --</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从右向左</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>3</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>* / %</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>4</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>+(加) -(减)</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>5</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div><< >> >>></div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>6</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>< <= > >= instanceof</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>7</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>==<span> !=</span></div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>8</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>&(按位与)</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>9</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>^</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>10</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>|</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>11</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>&&</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>12</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>||</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从左向右</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>13</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>?:</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85" valign="top"><div>从右向左</div></td></tr><tr><td style="border-bottom: black 1pt solid; border-left: black 1pt solid; padding-bottom: 0cm; padding-left: 5.4pt; width: 42.55pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="57" valign="top"><div>14</div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 169.8pt; padding-right: 5.4pt; border-top: medium none; border-right: windowtext 1pt solid; padding-top: 0cm;" width="226" valign="top"><div>= += -= *= /= %= &= |= ^= ~= <<= >>=<span> >>>=</span></div></td><td style="border-bottom: black 1pt solid; border-left: medium none; padding-bottom: 0cm; padding-left: 5.4pt; width: 64.05pt; padding-right: 5.4pt; border-top: medium none; border-right: black 1pt solid; padding-top: 0cm;" width="85"><div>从右向左</div></td></tr></tbody></table>
说明:
1、 该表中优先级按照从高到低的顺序书写,也就是优先级为1的优先级最高,优先级14的优先级最低。
2、 结合性是指运算符结合的顺序,通常都是从左到右。从右向左的运算符最典型的就是负号,例如3+-4,则意义为3加-4,符号首先和运算符右侧的内容结合。
3、 instanceof作用是判断对象是否为某个类或接口类型,后续有详细介绍。
4、 注意区分正负号和加减号,以及按位与和逻辑与的区别
其实在实际的开发中,不需要去记忆运算符的优先级别,也不要刻意的使用运算符的优先级别,对于不清楚优先级的地方使用小括号去进行替代,示例代码:
~~~
int m = 12;
int n = m << 1 + 2;
int n = m << (1 + 2); //这样更直观
~~~
这样书写代码,更方便编写代码,也便于代码的阅读和维护。
- 前言
- (1)序言
- (2)程序设计是什么?
- (3)你适合学习程序设计吗?
- (4)如何学好程序设计?
- (5)程序设计介绍小结
- (6)计算机软件基本概念
- (7)进制的概念
- (8)计算机内部的数据表达
- (9)网络编程基础
- (10)Java语言简介
- (11)JDK的获得、安装和配置
- (12)第一个HelloWorld程序
- (13)Eclipse基本使用
- (14)Eclipse基础使用进阶
- (15)如何学好Java语法
- (16)代码框架、关键字和标识符
- (17)基本数据类型
- (18)变量和常量
- (19)数据类型转换
- (20)空白、语句结束和注释
- (21)算术运算符
- (22)比较运算符
- (23)逻辑运算符
- (24)赋值运算符
- (25)位运算符
- (26)移位运算符
- (27)其它运算符
- (28)运算符优先级
- (29)表达式
- (30)流程控制基础
- (31)if语句语法(1)
- (32)if语句语法(2)
- (33)if语句语法(3)
- (34)switch语句语法
- (35)while语句语法
- (36)do-while语句语法
- (37)for语句语法
- (38)break和continue语句
- (39)流程控制综合示例1
- (40)流程控制综合示例2
- (41)流程控制综合示例3
- (42)流程控制综合练习
- (43)数组概述
- (44)数组基础语法
- (45)数组使用示例1
- (46)数组使用示例2
- (47)数组使用示例3
- (48)多维数组基础
- (49)多维数组使用示例1
- (50)多维数组使用示例2
- (51)多维数组练习
- (52)方法声明
- (53)方法声明示例
- (54)方法调用
- (55)方法重载和参数传递
- (56)方法练习
- (57)面向对象基础
- (58)类(一)
- (59)类(二)
- (60)对象
- (61)面向对象设计方法和面向对象特性(一)
- (62)继承(二)
- (63)多态性
- (64)访问控制符、修饰符和其它关键字
- (65)static修饰符
- (66)final修饰符
- (67)this和super
- (68)抽象类和接口(一)
- (69)抽象类和接口(二)
- (70)抽象类和接口(三)
- (71)内部类简介
- (72)包的概念
- (73)JDK文档使用
- (74)java.lang包介绍1
- (75)String类使用
- (76)StringBuffer类和System类
- (77)包装类
- (78)时间和日期处理
- (79)Random随机处理
- (80)集合框架简述
- (81)异常处理概述
- (82)异常处理语法1
- (83)异常处理语法2
- (84)IO简介
- (85)IO类体系
- (86)文件操作之File类使用
- (87)文件操作之读取文件
- (88)文件操作之写文件
- (89)读取控制台输入
- (90)装饰流使用1
- (91)装饰流使用2
- (92)IO使用注意问题
- (93)多线程基础
- (94)多线程实现方式1
- (95)多线程实现方式2
- (96)多线程使用示例1
- (97)多线程使用示例2
- (98)多线程问题及处理1
- (99)多线程问题及处理2
- (100)多线程问题及处理3
- (101)网络编程概述
- (102)网络编程技术1
- (103)网络编程技术2
- (104)网络编程技术3
- (105)网络编程技术4
- (106)网络编程技术5
- (107)网络协议概念
- (108)网络编程示例1
- (109)网络编程示例2
- (110)网络编程小结