ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## Java编程那些事儿29—表达式 郑州游戏学院 陈跃峰 出自:[http://blog.csdn.net/mailbomb](http://blog.csdn.net/mailbomb) **4.8 表达式** 由运算符和变量、常数或常量组成的式子称为表达式。例如2+3,a*b等。表达式是组成程序的基本单位,也是程序运算时的基本单位。 在程序代码内部,每个表达式都有自己对应的数据类型,具体参看下表: 表达式结果类型 <table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: 62.1pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" border="1"><tbody><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 35.45pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="47"> <div align="center">序号</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 92.15pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="123"> <div align="center">运算符</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">结果类型</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 35.45pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" width="47" rowspan="4"> <div align="center">1</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 92.15pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="123"> <div align="center">算术运算符</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" width="113" rowspan="4"> <div align="center">数字型</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 92.15pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="123"> <div align="center">位运算符</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 92.15pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="123"> <div align="center">移位运算符</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 92.15pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="123"> <div align="center">递增、递减运算符</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 35.45pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" width="47" rowspan="2"> <div align="center">2</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 92.15pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="123"> <div align="center">比较运算符</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" width="113" rowspan="2"> <div align="center">布尔型</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 92.15pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="123"> <div align="center">逻辑运算符</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 35.45pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="47"> <div align="center">3</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 92.15pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="123"> <div align="center">赋值运算符</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">和变量类型一致</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 35.45pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="47"> <div align="center">4</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 92.15pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="123"> <div align="center">条件运算符</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 3cm; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="113"> <div align="center">和两个值中类型高的一致</div> </td> </tr></tbody></table> 对于序号1和2的运算符组成的表达式,由于比较直观,就不再单独举例了,对于3和4说明如下: ~~~                    int n = 10;                    int m = 10;                    n =( m = 10);   //则表达式m=10的类型是变量m的类型,也是m的值                    boolean b = false;                    boolean b1 = true;                    b = (b1 = true); //则表达式b1 = true的类型是布尔型,值是true                    double d; d = 10 > 0 ? 1.0 : 2; //由于1.0是double型,2是整数型,则表达式的类型是double ~~~ 对于由多个运算符组成的表达式,其最终的类型由最后一个运算符决定。 在实际的程序代码中,大部分的表达式不能单独成为代码中的一行,否则程序会提示语法错误,例如: ~~~                    int a = 10;                    int b = 20;                    a + b;    //不能单独成行 ~~~ 在表达式中,能够单独成行的运算符包括赋值运算符和递增、递减运算符。 **4.9 总结** 本部分系统的讲解了Java语言中运算符的知识,并且介绍了实际使用过程中需要注意的问题,在学习时需要熟记每种运算符的作用,然后在实际项目中根据需要使用对应的运算符来实现程序的功能。