Relational Operators in Java

Video Tutorial
FREE
Relational Operators thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

Relational operators in Java are binary Java operators used to check the relations between two operands. They return a boolean value (true or false) by comparing the two operands. Java supports a variety of relational operators, including greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), equal to (==), and not equal to (!=). These operators are commonly used in conditional statements, loops, and other programming constructs to make decisions based on the values of variables or expressions.

What are Relational Operators in Java?

In programming, we often need to check the relation between two operands such as equality, in-equality, etc. Relational operators in Java are used to check the relations between two operands. After comparison, the relational operators return a boolean value. Relational operators are mainly used for conditional checks in if, else, for, while, etc.

Syntax

The syntax for relational operators in Java is given below:

  • operand1 - The first variable to be compared
  • operand2 - The second variable to be compared
  • relational_operator - The relational operator to check relation between operand1 and operand2.

Types of Relational Operators in Java

There are six types of relational operators in Java, and they can be found below:

  • Equal To (==) - Checks if two operands are equal
  • Not Equal To (!=) - Checks if two operands are not equal
  • Greater Than (>) - Checks if one operand is greater than the other
  • Less Than (<) - Checks if one operand is less than the other
  • Greater Than or Equal To (>=) - Checks if one operand is either greater than or equal to the other.
  • Less Than or Equal To (<=) - Checks if one operand is either less than or equal to the other.

Equal To (==)

The Equal To (==) operator checks if two operands are equal. It returns true if two operands are equal; else false. The syntax of the == operator is:

Not Equal To (!=)

The Not Equal To (!=) operator checks if two operands are not equal. It returns true if two operands are not equal; else false. The syntax of the != operator is:

Greater Than (>)

The Greater Than (>) operator checks if one operand is greater than the other. It returns true if the first operand is greater than the other; else false. The syntax for the > operator is:

Less Than (<)

The Less Than (<) operator checks if one operand is less than the other. It returns true if the first operand is less than the other; else false. The syntax for the < operator is:

Greater Than or Equal To (>=)

The Greater Than or Equal To (>=) operator checks whether one operand is greater than or equal to the other. It returns true if the first operand is greater than or equal to the other; else false. The syntax for the >= operator is:

Less Than or Equal To (<=)

The Less Than or Equal To (<=) operator checks if one operand is either less than or equal to the other. It returns true if the first operand is less than or equal the other; else false. The syntax for the <= operator is:

Examples of Relational Operators in Java

Equal To (==)

We used the == operator to check if two values are equal.

Output

Not Equal To (!=)

We used the != operator to check if the two values were not equal equal.

Output:

Greater Than (>)

We used the > operator to check if one value is greater than the other.

Output:

Less Than (<)

We used the < operator to check if one value is less than the other.

Output:

Greater Than or Equal To (>=)

We used the >= operator to check whether one value is greater than or equal to the other.

Output:

Less Than or Equal To (<=)

We used the <= operator to check if one value is either less than or equal to the other.

Output:

Conclusion

  • Relational operators in Java are binary operators used to check the relations between two operands
  • Relational operators return a boolean value
  • There are six relational operators in Java: ==, !=, <, >, <=, and >=
  • Relational operators are mainly used for conditional checks if, else, for, while, etc.