Java: Relational Operators

Relational Operators

This Java video tutorial examines the nature and uses of relational operators in Java. A relational operator is an operator that takes two values and evaluates whether they fulfill the specified relationship. If so, the boolean value true is returned. Otherwise, the operator returns false.

Six Relational Operators

Six Operators

Above, we have the six relational operators and their Java representations; notice that four of the the Java operators are represented by two characters. The equal to operator is represented by a double equals sign because the single equals sign is used for the assignment operator. The not equal to, greater than or equal to, and the less than or equal to are represented by !=, >=, and <=, respectively. This is because the normal characters for these operators are not available on the keyboard.

Program 1

package test;

public class Test {
    public static void main(String[] args) {
        System.out.print("Is 6 < 2? ");
        System.out.println(6 < 2);
        System.out.print("Is 6 > 2? ");
        System.out.println(6 > 2);
    }
}

Output 1

Program 1 Output

In the program above, we demonstrate the usage of the relational less than operator with two sets of arguments: one that returns false and one that returns true. First, we print out a question. Then we print out the answer by evaluating the relational operator. As we see in the output, the words false and true are printed for the boolean values that the operators returns.

Operators and Sets

When we apply a relational operator to a variable, we have a set of values for which the operator is true. Above, we have six example sets that are graphed on the number line. The red regions indicate the values of X for which the operator is true. Notice that each pair of successive operators generates setwise complements. That is, the sets have exactly the opposite members.

Program 2

package test;

public class Test {
    public static void main(String[] args) {
        double dX = 3.0;
        System.out.print("dX < 5 ? " + (dX < 5));
        System.out.println(" if dX = " + dX);
        dX = 4.0;
        System.out.print("dX < 5 ? " + (dX < 5));
        System.out.println(" if dX = " + dX);
        dX = 5.0;
        System.out.print("dX < 5 ? " + (dX < 5));
        System.out.println(" if dX = " + dX);
        dX = 6.0;
        System.out.print("dX < 5 ? " + (dX < 5));
        System.out.println(" if dX = " + dX);
        dX = 7.0;
        System.out.print("dX < 5 ? " + (dX < 5));
        System.out.println(" if dX = " + dX);
    }
}

Output 2

Program 2 Output

In the second program above, we apply the relational operator less than to the value 5 and the variable dX while it takes on a series of values. In the output pane, we see that the operator evaulates to true for some values and false for others. The set of values for which the operator is true can be represented on the number line, as we showed before.

Program 3

package test;

public class Test {
    public static void main(String[] args) {
        double dX = 3.0;
        if (dX != 4.0) {
            System.out.println("dX != 4.0");
        } else {
            System.out.println("dX == 4.0");
        }
    }
}

Output 3

Program 3 Output

Our third program illustrates the most powerful application of a relational operator. In this program, we put the relational operator into a conditional if statement that executes one block of code if the operator returns true and another block if the operator returns false. Since the variable was initialized to 3.0, the first print statement is executed.