Home ยป Java Relational Operators Tutorial with Code Examples

Java Relational Operators Tutorial with Code Examples

Relational operators in Java are used to compare two values and determine the relationship between them. They are also called comparison operators because they compare operands and return a boolean value: true or false.

Understanding how to use relational operators is crucial for making decisions and controlling the flow of the program.

This tutorial will explain the Java relational operators with examples to help you understand how they work.

Table of Contents:

1. Introduction to Relational Operators

Relational operators allow you to compare two operands. These comparisons are commonly used in control flow structures like if, while, and for statements. The result of a relational operation is always a boolean valueโ€”either true or false.

2. List of Relational Operators in Java

Java supports the following relational operators:

Operator Description
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to

Example 1: Greater Than

public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        
        boolean result = a > b;
        System.out.println("Is a greater than b? " + result);  // Output: true
    }
}

Explanation:

a > b checks if a is greater than b, which is true because 10 > 5.

Less Than (<)

The < operator checks if the left operand is less than the right operand.

Example 2: Less Than

public class Main {
    public static void main(String[] args) {
        int a = 3;
        int b = 8;
        
        boolean result = a < b;
        System.out.println("Is a less than b? " + result);  // Output: true
    }
}

Greater Than or Equal To (>=)

The >= operator checks if the left operand is greater than or equal to the right operand.

Example 3: Greater Than or Equal To

public class Main {
    public static void main(String[] args) {
        int a = 7;
        int b = 7;
        
        boolean result = a >= b;
        System.out.println("Is a greater than or equal to b? " + result);  // Output: true
    }
}

Explanation:

a >= b evaluates to true because 7 is equal to 7.

Less Than or Equal To (<=)

The <= operator checks if the left operand is less than or equal to the right operand.

Example 4: Less Than or Equal To

public class Main {
    public static void main(String[] args) {
        int a = 4;
        int b = 9;
        
        boolean result = a <= b;
        System.out.println("Is a less than or equal to b? " + result);  // Output: true
    }
}

Equal To (==)

The == operator checks if two operands are equal.

Example 5: Equal To

public class Main {
    public static void main(String[] args) {
        int a = 5;
        int b = 5;
        
        boolean result = a == b;
        System.out.println("Is a equal to b? " + result);  // Output: true
    }
}

Explanation:

a == b checks if a is equal to b, which is true because both have the value 5.

Not Equal To (!=)

The != operator checks if two operands are not equal.

Example 6: Not Equal To

public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 20;
        
        boolean result = a != b;
        System.out.println("Is a not equal to b? " + result);  // Output: true
    }
}

Explanation:

a != b evaluates to true because 10 is not equal to 20.

4. Using Relational Operators in Conditional Statements

Relational operators are commonly used in conditional statements such as if, while, and for loops to control the flow of the program.

Example 7: Using Relational Operators in an if Statement

public class Main {
    public static void main(String[] args) {
        int age = 18;

        // Check if age is greater than or equal to 18
        if (age >= 18) {
            System.out.println("You are eligible to vote.");
        } else {
            System.out.println("You are not eligible to vote.");
        }
    }
}

Output:

You are eligible to vote.

In this example, the program checks if the age is greater than or equal to 18. If true, the message “You are eligible to vote.” is printed.

5. Relational Operators with Different Data Types

Relational operators work with different data types, such as int, double, and char.

Example 8: Relational Operators with double

public class Main {
    public static void main(String[] args) {
        double a = 10.5;
        double b = 9.8;

        System.out.println(a > b);  // Output: true
    }
}

Example 9: Relational Operators with char

public class Main {
    public static void main(String[] args) {
        char letterA = 'A';
        char letterB = 'B';

        System.out.println(letterA < letterB);  // Output: true
    }
}

Explanation:

char values are compared based on their Unicode values, and ‘A' has a lower Unicode value than ‘B'.

6. Operator Precedence with Relational Operators

In Java, relational operators have a lower precedence than arithmetic operators but higher precedence than logical operators. Therefore, arithmetic operations are evaluated first.

Example 10: Operator Precedence

public class Main {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        int c = 2;

        boolean result = a > b + c;  // b + c is evaluated first, then a > result
        System.out.println(result);  // Output: true
    }
}

Explanation:

The expression b + c is evaluated first (5 + 2 = 7), and then the comparison a > 7 is made, resulting in true.

7. Common Use Cases for Relational Operators

Example 11: Checking for Odd or Even Numbers

public class Main {
    public static void main(String[] args) {
        int number = 11;

        // Check if the number is even
        if (number % 2 == 0) {
            System.out.println(number + " is even.");
        } else {
            System.out.println(number + " is odd.");
        }
    }
}

Explanation:

The modulus operator (%) is used to determine if a number is divisible by 2. If the remainder is 0, the number is even.

Example 12: Validating User Age for Voting Eligibility

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter your age:");
        int age = scanner.nextInt();

        // Check if the user is eligible to vote
        if (age >= 18) {
            System.out.println("You are eligible to vote.");
        } else {
            System.out.println("You are not eligible to vote.");
        }
    }
}

Explanation:

This example uses the relational operator >= to check if the user is old enough to vote.

Conclusion

Relational operators are essential for comparing values in Java and play a critical role in controlling the flow of a program through conditions and loops.

Understanding how to use relational operators like >, <, >=, <=, ==, and != allows you to implement decision-making in your applications effectively.

By combining these operators with conditional statements, you can develop complex logic that makes your programs dynamic and responsive.

You may also like