Community for developers to learn, share their programming knowledge. Register!
Java Operators

Java Relational Operators


Java is a powerful programming language that enables developers to build robust applications. Understanding the various operators in Java is essential for writing efficient and effective code. In this article, you can get training on Java relational operators, which are pivotal in making comparisons between two values. These operators are fundamental in controlling the flow of programs and are used extensively in conditional statements.

Introduction to Relational Operators

Relational operators in Java allow you to compare two values or expressions. The result of these comparisons returns a boolean value—either true or false. These operators are crucial in decision-making processes within your code, as they determine how your program will behave based on specific conditions.

Java provides six primary relational operators:

  • Equal to (==)
  • Not Equal to (!=)
  • Greater than (>)
  • Less than (<)
  • Greater than or Equal to (>=)
  • Less than or Equal to (<=)

Each of these operators plays a unique role and is used in various scenarios during programming. Let's explore each operator in detail.

Equal to Operator (==)

The equal to operator (==) checks if two values are the same. This operator can be used with primitive data types as well as reference types. It’s important to remember that when comparing objects, == checks for reference equality, meaning it checks if both references point to the same object in memory.

Example:

int a = 5;
int b = 5;

if (a == b) {
    System.out.println("a is equal to b");
}

In this case, the output will be a is equal to b since both a and b hold the same value.

Not Equal to Operator (!=)

The not equal to operator (!=) is used to determine if two values are different. If the values are not equal, the expression evaluates to true; otherwise, it evaluates to false.

Example:

int x = 10;
int y = 20;

if (x != y) {
    System.out.println("x is not equal to y");
}

Here, the output will be x is not equal to y because x and y hold different values.

Greater than Operator (>)

The greater than operator (>) checks if the value on the left is greater than the value on the right. This operator is often used in scenarios where you need to compare numerical values, such as in loops or conditional statements.

Example:

int num1 = 15;
int num2 = 10;

if (num1 > num2) {
    System.out.println("num1 is greater than num2");
}

The output will be num1 is greater than num2, as 15 is indeed greater than 10.

Less than Operator (<)

Conversely, the less than operator (<) checks if the value on the left is less than the value on the right. This operator is widely used in conditional expressions and control statements.

Example:

int a = 4;
int b = 8;

if (a < b) {
    System.out.println("a is less than b");
}

The output will be a is less than b because 4 is less than 8.

Greater than or Equal to Operator (>=)

The greater than or equal to operator (>=) checks if the value on the left is either greater than or equal to the value on the right. This operator is useful in scenarios where you want to include the boundary value in your condition.

Example:

int score = 85;
int passingScore = 75;

if (score >= passingScore) {
    System.out.println("You have passed!");
}

The output will be You have passed! since 85 is greater than 75.

Less than or Equal to Operator (<=)

Similarly, the less than or equal to operator (<=) checks if the value on the left is either less than or equal to the value on the right. This operator is essential in conditions where you want to include the limiting value.

Example:

int age = 18;
int votingAge = 18;

if (age <= votingAge) {
    System.out.println("You are eligible to vote.");
}

The output will be You are eligible to vote. as 18 is equal to the voting age.

Using Relational Operators in Conditional Statements

Relational operators are primarily used in conditional statements to control the flow of execution in Java programs. The most common constructs that utilize relational operators are if statements, while loops, and for loops.

Example of an If Statement:

int temperature = 30;

if (temperature > 25) {
    System.out.println("It's a warm day.");
} else {
    System.out.println("It's a cool day.");
}

In this example, the program checks if the temperature is greater than 25. If true, it prints It's a warm day.; otherwise, it prints It's a cool day..

Example of a For Loop:

for (int i = 0; i < 10; i++) {
    if (i % 2 == 0) {
        System.out.println(i + " is even.");
    } else {
        System.out.println(i + " is odd.");
    }
}

In this case, the loop iterates through numbers 0 to 9, using the equal to operator to check if i is even or odd, showcasing how relational operators can dictate the flow of control within loops.

Summary

In conclusion, Java relational operators serve as essential tools for comparing values and controlling program flow. The six primary relational operators—==, !=, >, <, >=, and <=—are indispensable for any Java developer, providing the foundational building blocks for conditional logic. Understanding how to effectively use these operators will enhance your ability to write efficient and effective Java code.

For further reference, you can explore the official Java documentation to deepen your understanding of operators in Java. By mastering relational operators, you pave the way for more complex programming constructs and logical decision-making in your applications.

Last Update: 09 Jan, 2025

Topics:
Java