Java运算符:相等与关系
时间:2020-01-09 10:34:46 来源:igfitidea点击:
Java中的相等和关系运算符用于确定一个操作数的值是否大于,小于,等于或者不等于另一操作数的值。这些运算符返回一个布尔值,该值确定比较是对还是错。例如if(5> 7)返回false,
其中asif(5 <7)返回true。
等式和关系运算符通常用于条件语句(如if-else)或者循环(如for循环)中,以确定何时在条件语句中执行逻辑以及执行多少次。
Java中的相等和关系运算符
| 操作符 | 说明 |
|---|---|
| == | 等于 |
| != | 不等于 |
| > | 大于 |
| >= | 大于或者等于 |
| < | 小于 |
| <= | 小于或者等于 |
等式和关系运算符的Java示例
public class ComparisonDemo {
public static void main(String[] args) {
int x = 8;
int y = 6;
int z = 8;
if (x > y){
System.out.println("value of x is greater than y");
}
if (x < y){
System.out.println("value of x is less than y");
}
if(x == y)
System.out.println("value of x is equal to y");
if(x != y)
System.out.println("value of x is not equal to y");
if(x >= z){
System.out.println("value of x is greater than or equal to z");
}
if(y <= z){
System.out.println("value of y is less than or equal to z");
}
}
}
输出:
value of x is greater than y value of x is not equal to y value of x is greater than or equal to z value of y is less than or equal to z
请注意,在测试两个基本值是否相等时,必须使用" =="而不是" ="(因为" ="是赋值运算符)。

