Relational operators in Java are used to compare two values. These operators return a boolean value of either true or false depending on the outcome of the comparison. The following relational operators are available in Java:
>
(greater than) – returns true if the value on the left is greater than the value on the right<
(less than) – returns true if the value on the left is less than the value on the right>=
(greater than or equal to) – returns true if the value on the left is greater than or equal to the value on the right<=
(less than or equal to) – returns true if the value on the left is less than or equal to the value on the right==
(equal to) – returns true if the value on the left is equal to the value on the right!=
(not equal to) – returns true if the value on the left is not equal to the value on the right
Here is an example of how these operators can be used in Java:
int x = 10;
int y = 20;
System.out.println(x > y); // prints false
System.out.println(x < y); // prints true
System.out.println(x >= y); // prints false
System.out.println(x <= y); // prints true
System.out.println(x == y); // prints false
System.out.println(x != y); // prints true
In the example above, we declare two variables x
and y
with the values 10 and 20, respectively. We then use the relational operators to compare these values and print the results. As you can see, the output of these statements is as expected based on the values of x
and y
.