Let’s learn relational operators in java.
Relational operators in java
Relational operators are used to check relation between two operands for equality, greater than, less than etc. Relational operators return boolean output. There are six relational operators,
- Equals to operator (==)
- Less than operator (<)
- Greater than operator (>)
- Not equals to operator (!=)
- Greater than equals to operator (>=)
- Less than equal to operator (<=)
Equals to operator (==): compares values between two operands for their equality. If both operands value have equal values it returns true else returns false.
Example:
class RelationalOperatorExample { public static void main(String[] args) { int x = 6; int y = 15; int z = 6; System.out.println("First Variable : " + x); System.out.println("Second Variable : " + y); System.out.println("Third Variable : " + z); // comparing first and second variable System.out.println("x == y : " + (x == y)); // comparing first and third variable System.out.println("x == z : " + (x == z)); } }
Output:
First Variable : 6
Second Variable : 15
Third Variable : 6
x == y : false
x == z : true
Less than operator (<): compares values between two operands for less than value. If operand value on left is less than the operand value on the right it returns true else returns false.
Example:
class RelationalOperatorExample { public static void main(String[] args) { int x = 9; int y = 19; int z = 6; System.out.println("First Variable : " + x); System.out.println("Second Variable : " + y); System.out.println("Third Variable : " + z); // comparing first and second variable System.out.println("x < y : " + (x < y)); // comparing first and third variable System.out.println("x < z : " + (x < z)); } }
Output:
First Variable : 9
Second Variable : 19
Third Variable : 6
x < y : true
x < z : false
Greater than operator (>): compares values between two operands for greater than value. If operand value on left is greater than operand value on the right it returns true else returns false.
Example:
class RelationalOperatorExample { public static void main(String[] args) { int x = 29; int y = 19; int z = 6; System.out.println("First Variable : " + x); System.out.println("Second Variable : " + y); System.out.println("Third Variable : " + z); // comparing first and second variable System.out.println("x > y : " + (x > y)); // comparing first and third variable System.out.println("x > z : " + (x > z)); } }
Output:
First Variable : 29
Second Variable : 19
Third Variable : 6
x > y : true
x > z : true
Not equals to operator (!=): compares values between two operands for their inequality. If operands value have unequal values then it returns true else returns false.
Example:
class RelationalOperatorExample { public static void main(String[] args) { int x = 6; int y = 9; int z = 6; System.out.println("First Variable : " + x); System.out.println("Second Variable : " + y); System.out.println("Third Variable : " + z); // comparing first and second variable System.out.println("x != y : " + (x != y)); // comparing first and third variable System.out.println("x != z : " + (x != z)); } }
Output:
First Variable : 6
Second Variable : 9
Third Variable : 6
x != y : true
x != z : false
Greater than equals to operator (>=):
This operator checks whether the first operand is greater than or equal to second operand or not. It returns true when operand at left hand side is greater than or equal to right hand side. Else returns false.
Example:
class RelationalOperatorExample { public static void main(String[] args) { int x = 19; int y = 19; int z = 9; System.out.println("First Variable : " + x); System.out.println("Second Variable : " + y); System.out.println("Third Variable : " + z); // comparing first and second variable System.out.println("x >= y : " + (x >= y)); // comparing first and third variable System.out.println("z >= x : " + (z >= x)); } }
Output:
First Variable : 19
Second Variable : 19
Third Variable : 9
x >= y : true
z >= x : false
Less than equals to operator (<=): This operator checks whether the first operand is less than or equal to second operand or not. It returns true when operand at left hand side is less than or equal to right hand side. Else returns false.
Example:
class RelationalOperatorExample { public static void main(String[] args) { int x = 9; int y = 9; int z = 6; System.out.println("First Variable : " + x); System.out.println("Second Variable : " + y); System.out.println("Third Variable : " + z); // comparing first and second variable System.out.println("x <= y : " + (x <= y)); // comparing first and third variable System.out.println("y <= z : " + (y <= z)); } }
Output:
First Variable : 9
Second Variable : 9
Third Variable : 6
x <= y : true
y <= z : false