Computers can make simple decisions. For example, a computer program can take one path of action if two variables, x and y, are equal, another path if x is greater than y, and yet another one if y is greater than x. The Java relational operators evaluate if a simple relationship between operands is true or false.
Operator
Action
Example
Result
<
less than
4 < 1
false
>
greater than
4 > 1
true
<=
less than or equal to
4 <= 1
false
>=
greater than or equal to
4 >= 1
true
==
equal to
4 == 1
false
!=
not equal to
4 != 1
true
package com.java_tutorial;
public class Relational_operators {
public static void main(String[] args) {
// TODO Auto-generated method stub
int x = 4;
int y = 1;
boolean z = x < y;
System.out.println("x < y Result: "+ z);
boolean za = x > y;
System.out.println("x > y Result: "+ za);
boolean zb = x <= y;
System.out.println("x <= y Result: "+ zb);
boolean zc = x >= y;
System.out.println("x >= y Result: "+ zc);
boolean zd = x == y;
System.out.println("x == y Result: "+ zd);
boolean ze = x != y;
System.out.println("x != y Result: "+ ze);
}
}
Comments (No)