(28)运算符优先级

最后更新于:2022-04-01 10:58:23

  ## 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>&lt;&lt; &gt;&gt; &gt;&gt;&gt;</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>&lt; &lt;= &gt; &gt;= 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>&amp;(按位与)</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>&amp;&amp;</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>= += -= *= /= %= &amp;= |= ^=  ~=  &lt;&lt;= &gt;&gt;=<span>   &gt;&gt;&gt;=</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); //这样更直观 ~~~ 这样书写代码,更方便编写代码,也便于代码的阅读和维护。  
';