The relational operators determine the relationship between two operands. e.g., 3 is greater than 2, 25 is less than 30.
The following six relational operators are supported by Java.
- Equal to (
==
) and Not equal to (!=
) can be applied on any type, including integers, floating-point numbers, characters and booleans.
- Ordering operators - less than, greater than, less than or equal and greater than or equal - can be used only on numeric types - integers, floating point numbers and characters. Booleans are not included.
- Single equal to (
=
) is assignment operator, which is diff from (==
) double equal to which compares two operands.
- The result produced by relational operator is always a
boolean
value. i.e. the value is either true
or false
class PrintNumberRelations
{
public static void main(String arg[])
{
int a = 4;
int b = 1;
boolean x = ( a > b );
boolean y = ( a <= b );
boolean z = ( a == b );
System.out.println(" The statement - a greater than b - is " + x);
System.out.println(" The statement - a is less than or equal to b - is " + y);
System.out.println(" The statement - a is equal to b - is " + z);
}
}
OUTPUTThe statement - a greater than b - is true
The statement - a is less than or equal to b - is false
The statement - a is equal to b - is false
DESCRIPTIONHere the relation between a
and b
is found using various relational operators. x
tells if a
is greater than b
(a > b
), y
tells if a
is less than or equal to b
(a <= b
) and z
tells if a
and b
are equal (a == b
).
THINGS TO TRY
- Add a new
boolean w
which tells if a
greater than or equal to b
and print the result.
- Add a new
boolean v
which tells if a
is not equal to b
and print the result.
- Add a new
boolean u
which tells if ((a + 2) * 3)
is less than ((b + 1) * 9)
and print the result.