Comparison Operators

Equality Operators

The equality operator determine if the operands are equal or not equal to.

Equality Operators

Operator Description
== Equal Operator
!= Not Equal

Example - Using Equality Operator

The equality operators are used with if statements. We haven't discussed if statements yet.

Let's test if dividing 4 by 2 will be equal to 0. When the comparison is done, isEven will be true.

boolean isEven;
isEven = (4 / 2 == 0);

Check if 5 divided by 2 will yield an odd result. isOdd will be true.

boolean isOdd;
isOdd = (5 / 2 == 0);

Relational Operators

Relational operators determine whether an operator is less than, greater than, less or equal to or greater than or equal to another operand.

Relational Operators

Operator Description
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Example - Using Relational Operators

Check if 3 is greater than 2. isGreater will be true.

boolean isGreater = (3 > 2);

Check if 2 is less than 3. isLess will be true after the statement is evaluated.

boolean isLess = (2 < 3);

Check if 3 is greater or equal to 2; result be true since 3 is greater than 2.

boolean result = (3 >= 2);

Check is 2 is less than or equal to 3. result will be true since 2 is less than or equal to 3.

boolean result = (2 <= 3);

results matching ""

    No results matching ""