Theory:Relational operators
# List of relational operators
Java provides six relational operators to compare numbers:
==
(equal to)!=
(not equal to)>
(greater than)>=
(greater than or equal to)<
(less than)<=
(less than or equal to)
The result of applying a relational operator to its operands will be boolean (
true
orfalse
) regardless of the types of operands.# Comparing integer numbers
Relational operators allow you to easily compare, among other things, two integer numbers. Here are some examples below:
int one = 1; int two = 2; int three = 3; int four = 4; boolean oneIsOne = one == one; // true boolean res1 = two <= three; // true boolean res2 = two != four; // true boolean res3 = two > four; // false boolean res4 = one == three; // false
1
2
3
4
5
6
7
8
9
10
11Relational operators can be used in mixed expressions together with arithmetic operators. In such expressions, relational operators have lesser priorities than arithmetic operators.
In the following example, first of all, two sums are calculated, and then they are compared using the operator
>
.int number = 1000; boolean result = number + 10 > number + 9; // 1010 > 1009 is true
1
2The
result
istrue
.# Joining relational operations using logical operators
In Java, you cannot write an expression like a <= b <= c. Instead, you should join two boolean expressions using logical operators like
||
and&&
.Here is an example:
number > 100 && number < 200; // it means 100 < number < 200
1Also, we can write parts of the expression in parentheses to improve readability:
(number > 100) && (number < 200);
1But parentheses are not necessary here because relational operators have a higher priority than logical operators.
Here is a more general example of variables.
int number = ... // it has a value int low = 100, high = 200; // borders boolean inRange = number > low && number < high; // joining two expressions using AND.
1
2
3
4The code checks if the value of
number
belongs to a range.So, logical operators allow you to join a sequence of relational operations into one expression.
# An example of a program
Suppose there are three children in the sports class. You want to check if their heights are arranged in descending order. The following program reads three integer numbers
h1
,h2
, andh3
and then checks ifh1 >= h2
andh2 >= h3
. Note that h means the height of a child.import java.util.Scanner; public class CheckDescOrder { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int h1 = scanner.nextInt(); int h2 = scanner.nextInt(); int h3 = scanner.nextInt(); boolean descOrdered = (h1 >= h2) && (h2 >= h3); System.out.println(descOrdered); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Here are several input-output pairs:
Input 1
185 178 172
1Output 1
true
1Input 2
181 184 177
1Output 2
false
1It is possible not to use an additional variable to store the boolean result before output:
System.out.println((h1 >= h2) && (h2 >= h3));
1But when your condition is quite long, it is hard to understand what the code does without some explanations. A variable with a good name provides such an explanation.