Operators

Relational Operators

🙋 Need help? Ask an expert now!

A relational operator is an operator that tests or defines a relationship between two operands. These include numerical equality such as "5 = 5" and inequalities such as "7<3". Please take a look at the six relational operators that follow.

RelationalOperatorExample.java
package exlcode;

public class RelationalOperatorExample {

  public static boolean exampleVariableOne = 10 < 15;
  public static boolean exampleVariableTwo = 10 > 15;
  public static boolean exampleVariableThree = 10 <= 10;
  public static boolean exampleVariableFour = 10 >= 15;
  public static boolean exampleVariableFive = 15 == 10;
  public static boolean exampleVariableSix = 15 != 10;

  public static void main(String[] args) {
    System.out.println(exampleVariableOne);
    System.out.println(exampleVariableTwo);
    System.out.println(exampleVariableThree);
    System.out.println(exampleVariableFour);
    System.out.println(exampleVariableFive);
    System.out.println(exampleVariableSix);
  }
}

All of the operators above return a true or false result. The first four operators are recognizable and common. They work the same way as you learned in math class. Check the text below for an explanation of the operators functions.

  1. > Tests whether the first integer is greater than the second integer.
  2. < Tests whether the first integer is smaller than the second integer.
  3. >= Tests whether the first integer is greater than or equal to the second integer.
  4. <= Tests whether the first integer is less than or equal to the second integer.
  5. == Tests whether the first integer is equal to the second integer.

Remember that '=' cannot be used to compare two values because a solo '=' is used only for assigning values to variables as we saw previously

  1. != Tests whether the first integer is different from the second integer.

Therefore, a statement like 5 != 7 will return a result of "true".

Edit Me on GitHub!

Application Question

#!exl::repl('index.prob.repl.yaml')