Java: Relational Operator

There is an operator who can check the relational conditions and satisfy according to its need. Conditions such as more significant than, less than, and equal to your name. These conditions are validated with the boolean response of true or false. We call such operator relational operator and are primarily used up in looping statements and if statements.

Syntax

variable1 relational operator variable2

Let's have a look in a more detailed manner.

1. Equal to Relational Operator (==)

This operator is solely used to check whether the operand on the left is similar to the operand on the right. If matches, it returns true else returns false.

Syntax

variable1 == variable2

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10;
 
        val3 = val1 + val2;

        if (val1 == val4)
        {
            System.out.println("True");
        }
        else
        {
            System.out.println("False");
        }
    }
}

Output: True

2. Not Equal to Relational Operator (!=)

The operator satisfies the condition of not equal, meaning the state returns true if the following operand is not equal to the operand on the left. Else if it matches, it returns false.

Syntax

variable1 != variable2 (! Negation is used for not equal term)

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10;
        val3 = val1 + val2;

        if (val1 != val4)
        {
            System.out.println("False");
        }
        else if (val1 != val2)
        {
            System.out.println("True");
        }
    }
}

Output: True

3. Greater Than Relational Operator (>)

This operator determines whether the first operand is greater than the second operator. Returns true if it is greater else false.

Syntax

Variable1 > Variable2

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10;
        val3 = val1 + val2;

        if (val1 > val2)
        {
            System.out.println("True");
        }
        else
        {
            System.out.println("False");
        }
    }
}

Output: False

4. Lesser Than Relational Operator (<)

This operator points out whether the first operand is Lesser than the second operator. Returns true if it is greater else false.

Syntax

Variable1 < Variable2

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10;
        val3 = val1 + val2;
    
        if (val1 < val2)
        {
            System.out.println("True");
        }
        else
        {
            System.out.println("False");
        }
    }
}

Output: True

5. Greater Than Or Equal Relational Operator (>=)

This operator points out whether the first operand is Greater than the second operator. Returns true if it is greater or equal. Else false.

Syntax

Variable1 >= Variable2

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10;
        val3 = val1 + val2;

        if (val1 >= val4)
        {
            System.out.println("True");
        }
        else
        {
            System.out.println("False");
        }
    }
}

Output: True

6. Lesser Than Or Equal To Relational Operator (<=)

This operator points out whether the first operand is smaller than the second operator. Returns true if it is Lesser or equal. Else false.

Syntax

Variable1 <= Variable2

Example

public class Test
{
    public static void main(String[] args)
    {
        int val1 = 10 , val2 = 20, val3, val4 = 10;
        val3 = val1 + val2;

        if (val1 <= val4)
        {
            System.out.println("True");
        }
        else
        {
            System.out.println("False");
        }
    }
}

Output: True

Thu, 10/14/2021 - 13:08

Authored by

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.
Tags