(22)比较运算符
最后更新于:2022-04-01 10:58:09
## Java编程那些事儿22—比较运算符
郑州游戏学院 陈跃峰
出自:[http://blog.csdn.net/mailbomb](http://blog.csdn.net/mailbomb)
**4.2 比较运算符**
比较运算符实现数据之间大小或相等的比较。
比较运算符运算的结果是一个boolean类型的值,如果比较结果成立则为true,否则为false。
Java语言中比较运算符的表示和功能见下表4-2。
表4-2 比较运算符
<table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: -3.95pt; 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: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <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: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <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: 190.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="254"> <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: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <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: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <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: 190.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="254"> <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: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <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: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <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: 190.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="254"> <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: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <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: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <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: 190.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="254"> <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: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <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: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <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: 190.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="254"> <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: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <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: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <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: 190.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="254"> <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: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <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: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <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: 190.6pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="254"> <div align="center">比较左侧数字是否不等于右侧数字</div> </td> </tr></tbody></table>
比较运算符的运算规则和现实中的规则一样。需要注意的问题主要有以下几个:
1. boolean类型只能比较相等和不相等,不能比较大小。
2. >=的意思是大于或等于,两者成立一个即可,所以5>=5成立。
3. 在数学上表示的区间[1,10),也就是数字大于等于1同时小于10,在程序中不能写成如下格式:1<=n<10,这种书写在语法上是错误的,如果需要表达这种区间,则参看4.3逻辑运算符实现部分。
4. 判断相等的符号是两个等号,而不是一个等号,这个需要特别小心。
比较运算使用的示例代码如下:
~~~
int a = 10;
boolean b = (a > 3); //该条件成立,则将值true赋值给变量b
boolean c = (b == true); //条件成立,结果为true
~~~
在实际代码中,数值、变量以及运算结果都可以直接参与比较,只是程序中为了增强可读性,有些时候需要将比较分开进行书写。
比较运算符是程序设计中实现数据比较的基础,也是很多逻辑实现的基础,在程序逻辑中,经常通过比较一定的条件,来判断后续的程序该如何执行。