In addition to the standard arithmetic
operators, Java allows us to compare numerical values using a set of familiar
constructs. These are the relational
operators and are as follows: < is less than
> is greater
than >= is greater
than or equal to <= is less than
or equal to != is not equal
to == is equal to The result of a relational expression, as
opposed to an arithmetic one, can only be either true or false. Both of these truth values are reserved
words in Java and are referred to as Boolean values. So
if we type the statement System.out.println(3 != 5); // does 3
not equal 5? will output true, while the statement System.out.println(3 != 3); will output false. Such statements can also include arithmetic
expressions. An example: System.out.println(10 % 3 == 12 / 8); To understand how this statement will be
evaluated, it’s useful to know that relational operators have lower
precedence than arithmetic operators.
This means that the modulus subexpression
will be evaluated first, yielding 1, then the division which also results in
1. Finally the comparison 1 == 1 is carried out, so the output will be the
Boolean value true. |