Java

Relational Operators in Java | Explained

In Java, the relational operators examine the relationship between variables/values. The relational operators return Boolean values after verifying the relation. If the relationship is correct then it would return true other, false. This category includes operators such as less than, greater than, equal and not equal, less than or equal to, greater than or equal. This article provides the working and usage of relational operators in the Java language.

How relational operators work in Java

The relational operators used in Java are alike to any other programming language. Each of the relational operators is defined here, and further usage would be provided in the upcoming section.

Equal to (==): This operator checks the equality relationship between values/variables in Java. The symbol of the operator is “==” and is practiced by following the syntax provided below:

a==b;

Not equal (!=): This is the opposite of the equal to the operator as it checks for the inequality of two variables/values. The symbol to use this operator is “!(=)” and the following syntax may be followed for this operator:

a!=b;

Greater than (>). The greater than operator checks whether the operand on the left side is greater than the other or not. The syntax of using the greater than operator is shown below:

a>b;

Greater than or equal to (>=): It shows whether a variable is higher or equal to another. The “greater than or equal to” is exercised using the symbol “>=” and it follows the syntax provided below.

a>=b;

Less than (<): This operator checks whether the first variable/value is less than the second value/variable or not. You may refer to the following statement to use this operator.

a<b;

Less than or equal to (<=): It is practiced to check the “less than or equal” to the relation between two operands using the following syntax:

a<=b;

How to use relational operators in Java

This section provides the usage of relational operators in Java. Later in this section, you would also get to know the working of relational operators in if-else statements and loops of Java programming.

Equal to (==): The following Java code makes use of the “equal to” operator between two variables and returns the false result as the variable x=2 is not equal to y=4.

package newpack;

public class RelOp {
public static void main(String[]args) {
       
        //declaring variables
        int x=2, y=4;
       
        System.out.println(x==y);
    }

}

The output of the code is provided below:

Not Equal to(!=): The following Java program, a=7 is not equal to b=9, but the not equal operator returned true in the output.

package newpack;

public class RelOp {
public static void main(String[]args) {
       
        //declaring variables
        int a=7, b=9;
       
        System.out.println(a!=b);
    }

}

The output of the code is displayed below:

Less than (<): The code provided below compares the variables c and d using the “less than” relational operator. As the value of variable c=10 is less than d=15, so the output would be true:

package newpack;

public class RelOp {
public static void main(String[]args) {
       
        //declaring variables
        int c=10, d=15;
       
        System.out.println(c<d);
    }

}

The output of the code is shown below:

Greater than (>): The Java code provided below makes use of the greater than operator on two variables (e and f). The integer 18 is stored in variable e whereas 12 is assigned to variable f: that shows the value of f is greater than e but we have checked whether f>e or not.

package newpack;

public class RelOp {
public static void main(String[]args) {
       
        //declaring variables
        int e=18, f=12;
       
        System.out.println(f>e);
    }

}

The output of the above code is false because the f<e, not f>e:

Greater than or equal to (>=): The code provided below exercises the greater than or equal to operator on two variables. The condition(x>=y) defined in the code is true thus the output is also true:

package newpack;

public class RelOp {
public static void main(String[]args) {
       
        //declaring variables
        int x=13, y=13;
       
        System.out.println(x>=y);
    }

}

The output of the code is shown below:

Less than or equal to (<=): This operator is practiced on two variables a and b. The values assigned to a and b are 5 and 8 respectively. The condition set is b<=a which is false thus the return would also be false.

package newpack;

public class RelOp {
public static void main(String[]args) {
       
        //declaring variables
        int a=5, b=8;
       
        System.out.println(b<=a);
    }

}

The output of the code can be seen in the image below:

Using relational operators in loops and if-else statements in Java

The most common practice of relational operators is to use them inside the loops and if-else conditional statements to build a condition.

For loop: The For loop is a widely used type of loop and the code written below shows the usage of For loops with relational operators.

package newpack;

public class RelOp {
public static void main(String[]args) {
       
        //declaring variables
        int z=2, i;
       
        //using for loop and <=
        for (i=0; i<=z; i++)
        {
            System.out.println("Condition is true");
        }
    }
}

The code is described as:

  • declaring z and i variables
  • using for loop and relational operator(less than or equal to)
  • printing a statement

The output of the code is shown below:

if-else and relational operators: The following code demonstrates the usage of if-else and relational operator (>).

package newpack;

public class RelOp {
public static void main(String[]args) {
       
        //declaring variables
        int a=4, b=3;
       
         
        //using if-else and relational operator
        if (a>b)
        {
            System.out.println("The condition is true");
        }
        else
        {
            System.out.println("False!");
        }
    }
}

The code is described here:

  • declaring variables
  • using the (a>b) as a condition in if statement
  • printing inside the if and else statements

The output of the code is shown below:

Conclusion

The relational operators in Java have the key responsibility of checking the condition and returning the result based on that condition check. Java relational operators are practiced in conditional statements and loops to make decisions in programs. This article demonstrates the working of relational operators. In addition, several examples are provided that illustrate the use of conditional operators individually as well as in conditional statements and loops.

About the author

Adnan Shabbir