发 帖  
原厂入驻New
JAVA语言的运算符的优先级
2008-12-6 00:29:13  2432
分享
<p><span style="FONT-FAMILY: 黑体"><font face="Verdana"><strong>JAVA语言的运算符的优先级</strong></font></span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">表达式的运算次序取决于表达式中各种运算符的优先级。优先级高的先运算,优先级低的后运算,另外还可用括号</span>“<span lang="EN-US" style="FONT-FAMILY: 宋体">()</span>”<span style="FONT-FAMILY: 宋体">改变表达式的运算次序。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt">Jave <span style="FONT-FAMILY: 宋体">语言规定的运算符优先级如表</span><span lang="EN-US" style="FONT-FAMILY: 宋体">3-3</span><span style="FONT-FAMILY: 宋体">所示,表中排在上面的运算符有较高的优先级,同一行中的运算符的优先级相同。如果在表达式中有括号(表达式中只使用圆括号),又有函数,则优先计算括号中的内容,其次再进行函数运算。</span></p>
<p>
<meta http-equiv=Content-Language content="zh-cn"/>
<style>
<!--
h2
        {margin:13.0pt 0cm; text-align:justi€€Fy;
        text-justify:inter-ideograph;
        line-height:173%;
        page-break-after:avoid;
        font-size:16.0pt;
        font-family:Arial;
        }
p.MsoNormalIndent
        {margin-bottom:.0001pt;
        text-indent:21.0pt;
        line-height:150%;
        font-size:12.0pt;
        font-family:"Times New Roman";
        margin-left:0cm; margin-right:0cm; margin-top:0cm}
p.MsoNormal
        {mso-style-parent:"";
        margin-bottom:.0001pt;
        text-align:justify;
        text-justify:inter-ideograph;
        font-size:10.5pt;
        font-family:"Times New Roman";
        margin-left:0cm; margin-right:0cm; margin-top:0cm}
h3
        {margin:13.0pt 0cm; text-align:justify;
        text-justify:inter-ideograph;
        line-height:173%;
        page-break-after:avoid;
        font-size:16.0pt;
        font-family:"Times New Roman";
        font-weight:normal}
-->
</style>
</p>
<p class="MsoNormal" style="TEXT-INDENT: 63.25pt"><b><span style="FONT-FAMILY: 宋体">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 表</span>3-3&nbsp; Jave<span style="FONT-FAMILY: 宋体">语言的运算符优先级</span></b></p>
<center><img src="http://61.183.17.54/java/jxkj/book/ch3/表3-3.gif"/></center>
<p>
<meta http-equiv=Content-Language content="zh-cn"/>
<style>
<!--
h2
        {margin:13.0pt 0cm; text-align:justify;
        text-justify:inter-ideograph;
        line-height:173%;
        page-break-after:avoid;
        font-size:16.0pt;
        font-family:Arial;
        }
p.MsoNormalIndent
        {margin-bottom:.0001pt;
        text-indent:21.0pt;
        line-height:150%;
        font-size:12.0pt;
        font-family:"Times New Roman";
        margin-left:0cm; margin-right:0cm; margin-top:0cm}
p.MsoNormal
        {mso-style-parent:"";
        margin-bottom:.0001pt;
        text-align:justify;
        text-justify:inter-ideograph;
        font-size:10.5pt;
        font-family:"Times New Roman";
        margin-left:0cm; margin-right:0cm; margin-top:0cm}
h3
        {margin:13.0pt 0cm; text-align:justify;
        text-justify:inter-ideograph;
        line-height:173%;
        page-break-after:avoid;
        font-size:16.0pt;
        font-family:"Times New Roman";
        font-weight:normal}
-->
</style>
</p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">按照表</span><span lang="EN-US" style="FONT-FAMILY: 宋体">3</span>-3<span style="FONT-FAMILY: 宋体">中的规定,下列表达式有惟一的求值次序:</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(</span><span lang="EN-US">1</span><span style="FONT-FAMILY: 宋体">)</span><span lang="EN-US">a + b*c</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(</span><span lang="EN-US">2</span><span style="FONT-FAMILY: 宋体">)</span><span lang="EN-US">a=b||c</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(</span><span lang="EN-US">3</span><span style="FONT-FAMILY: 宋体">)</span><span lang="EN-US">a + b &lt; c&amp;&amp;d==e</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(</span><span lang="EN-US">4</span><span style="FONT-FAMILY: 宋体">)</span><span lang="EN-US">a=8-2*3&lt;4&amp;&amp;5&lt;2</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">表达式(</span>1<span style="FONT-FAMILY: 宋体">)中,运算符“</span>*<span style="FONT-FAMILY: 宋体">”比“</span>+<span style="FONT-FAMILY: 宋体">”的优先级高,所以先求</span>b*c<span style="FONT-FAMILY: 宋体">的值之后再与</span>a<span style="FONT-FAMILY: 宋体">的值相加。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">表达成(</span>2<span style="FONT-FAMILY: 宋体">)中,逻辑运算符“</span>||<span style="FONT-FAMILY: 宋体">”比赋值运算符优先级高,所以先对</span>b||c<span style="FONT-FAMILY: 宋体">求值,然后将结果赋给变量</span>a<span style="FONT-FAMILY: 宋体">。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">表达式(</span>3<span style="FONT-FAMILY: 宋体">)中,四个运算符中“</span>+<span style="FONT-FAMILY: 宋体">”优先级最高、“</span>&lt;<span style="FONT-FAMILY: 宋体">”</span> <span style="FONT-FAMILY: 宋体">次之,“</span>&amp;&amp;<span style="FONT-FAMILY: 宋体">”最低,所以求值次序是先求出式子</span>a + b<span style="FONT-FAMILY: 宋体">的值,然后将这个值与</span>c<span style="FONT-FAMILY: 宋体">的值作“</span>&lt;<span style="FONT-FAMILY: 宋体">”比较,然后求</span>d==e<span style="FONT-FAMILY: 宋体">的值,最后做“</span>&amp;&amp;<span style="FONT-FAMILY: 宋体">”运算。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">表达式(</span>4<span style="FONT-FAMILY: 宋体">)中,先求出</span>2*3<span style="FONT-FAMILY: 宋体">的值为</span>6<span style="FONT-FAMILY: 宋体">,然后求出</span>8-6<span style="FONT-FAMILY: 宋体">的值为</span>2<span style="FONT-FAMILY: 宋体">,再求出</span>2&lt;4<span style="FONT-FAMILY: 宋体">的值为</span>true<span style="FONT-FAMILY: 宋体">,</span>5&lt;2<span style="FONT-FAMILY: 宋体">的值为</span>false<span style="FONT-FAMILY: 宋体">,然后再求出</span>true &amp;&amp; false<span style="FONT-FAMILY: 宋体">的值为</span>false<span style="FONT-FAMILY: 宋体">,最后将</span>false<span style="FONT-FAMILY: 宋体">赋值给变量</span>a<span style="FONT-FAMILY: 宋体">。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">在</span>Java<span style="FONT-FAMILY: 宋体">语言中要记住这么多运算符的优先级是比较困难的,而且上述表达式的写法比较难以理解并容易产生错误。因此在写程序时应该养成一种良好的习惯:除非是简单的直观运算,应该尽量使用括号。上述表达式(</span>1<span style="FONT-FAMILY: 宋体">)是可取的,表达式(</span>2<span style="FONT-FAMILY: 宋体">)、(</span>3<span style="FONT-FAMILY: 宋体">)和(</span>4<span style="FONT-FAMILY: 宋体">)应分别改写为</span>a=(b||c)<span style="FONT-FAMILY: 宋体">、</span>((a+b) &lt;c)&amp;&amp;(d==e)<span style="FONT-FAMILY: 宋体">和</span>a=( ((8-2*3) &lt;4)&amp;&amp;(5&lt;2))<span style="FONT-FAMILY: 宋体">。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">另外,要尽量不写太长的表达式,过长的表达式容易引起对求值次序的误解。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">例如:</span><span lang="EN-US">2*Math.sqrt</span><span lang="EN-US" style="FONT-FAMILY: 宋体">(</span><span lang="EN-US">Math.pow(3,2)*25)+4</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 52.5pt"><span style="FONT-FAMILY: 宋体">↑<span lang="EN-US">&nbsp;&nbsp; </span>↑<span lang="EN-US">&nbsp;&nbsp; </span>↑ &nbsp;&nbsp;↑<span lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</span>↑ ↑</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 53.25pt"><span style="FONT-FAMILY: 宋体">⑤<span lang="EN-US">&nbsp;&nbsp; </span>④<span lang="EN-US">&nbsp;&nbsp; </span>③<span lang="EN-US">&nbsp;&nbsp; </span>①<span lang="EN-US">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span>② ⑥</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">运算的优先顺序如小圆圈的数字所示,这个表达式的运算结果是</span>34.0<span style="FONT-FAMILY: 宋体">。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">对于表达式再作以下几点说明:</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(</span>1<span style="FONT-FAMILY: 宋体">)数学上的表达式</span>a<span style="FONT-FAMILY: 宋体">≥</span>b<span style="FONT-FAMILY: 宋体">≥</span>c<span style="FONT-FAMILY: 宋体">及</span>-5<span style="FONT-FAMILY: 宋体">≤</span>x<span style="FONT-FAMILY: 宋体">≤</span>5<span style="FONT-FAMILY: 宋体">,用</span>JAVA<span style="FONT-FAMILY: 宋体">语言表示,必须分别写成(</span>a<span style="FONT-FAMILY: Courier="Courier"  New">&gt;</span>=b) <span lang="EN-US" style="FONT-FAMILY: 宋体">&amp;&amp; </span>(b&gt;=c)<span style="FONT-FAMILY: 宋体">及</span>(-5<span style="FONT-FAMILY: Courier="Courier"  New">&lt;</span>=x) &amp;&amp; (x<span style="FONT-FAMILY: Courier="Courier"  New">&lt;=5)</span><span style="FONT-FAMILY: 宋体">。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(</span>2<span style="FONT-FAMILY: 宋体">)逻辑运算</span>a <span lang="EN-US">|</span>| b<span style="FONT-FAMILY: 宋体">是好的表示形式。但若写成</span>a|<span lang="EN-US">|</span>b<span style="FONT-FAMILY: 宋体">则是不好的写法,在操作数与运算符之间至少留一空格可以使程序更加清晰。</span></p>
<p class="MsoNormal" style="TEXT-INDENT: 21pt"><span style="FONT-FAMILY: 宋体">(</span>3<span style="FONT-FAMILY: 宋体">)有时为了表达清楚,可以引进多余的括号,如:</span>x<span style="FONT-FAMILY: Courier="Courier"  New">&gt;=5 </span>||<span style="FONT-FAMILY: Courier="Courier"  New"> </span>y <span style="FONT-FAMILY: Courier="Courier"  New">&lt;</span><span style="FONT-FAMILY: 宋体">0是正确的表示形式,但引进括号后(</span>x<span style="FONT-FAMILY: Courier="Courier"  New">&gt;=5</span><span style="FONT-FAMILY: 宋体">) </span>|| (y <span style="FONT-FAMILY: Courier="Courier"  New">&lt;</span><span style="FONT-FAMILY: 宋体">0),可以表达得更加清楚。</span></p>
0
2008-12-6 00:29:13   评论 分享淘帖

只有小组成员才能发言,加入小组>>

116个成员聚集在这个小组

加入小组

创建小组步骤

关闭

站长推荐 上一条 /7 下一条

快速回复 返回顶部 返回列表