这些运算符用于检查关系,如大于等于,小于等于。它们在比较之后返回布尔结果,并广泛用于循环语句以及有条件的else语句。一般格式是,
variable relation_operator value
一些关系运营商是 -
// Java program to illustrate // relational operators public class operators { public static void main(String[] args) { int a = 20, b = 10; String x = "Thank", y = "Thank"; int ar[] = { 1, 2, 3 }; int br[] = { 1, 2, 3 }; boolean condition = true; //various conditional operators System.out.println("a == b :" + (a == b)); System.out.println("a < b :" + (a < b)); System.out.println("a <= b :" + (a <= b)); System.out.println("a > b :" + (a > b)); System.out.println("a >= b :" + (a >= b)); System.out.println("a != b :" + (a != b)); // Arrays cannot be compared with // relational operators because objects // store references not the value System.out.println("x == y : " + (ar == br)); System.out.println("condition==true :" + (condition == true)); } }
输出:
a == b:false a = b:true a!= b:true x == y:false condition== true:true