VOOZH about

URL: https://www.geeksforgeeks.org/java/java-relational-operators-with-examples/

⇱ Java Relational Operators with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java Relational Operators with Examples

Last Updated : 9 Jun, 2026

Relational Operators in Java are used to evaluate the relationship between two operands by comparing their values. They help determine whether a specific condition is satisfied and produce a boolean result that can be used to control program execution.

  • Relational operators are essential for implementing decision-making and branching logic.
  • They return a boolean value (true or false) based on the comparison result.
  • These operators are commonly used in if, if-else, switch, while, for, and other control flow statements.

Output
true

Explanation: The < operator checks whether a is less than b; since 10 < 20, the result is true

Tpyes of Relational operators in Java

Equal To Operator (==)

The Equal To (==) operator is used to check whether two operands have the same value. It returns true if both operands are equal; otherwise, it returns false.

  • Returns true when both operands have equal values.
  • Commonly used in conditional statements for comparison.

Syntax:

operand1 == operand2


Output
Var1 = 5
Var2 = 10
Var3 = 5
var1 == var2: false
var1 == var3: true

Not Equal To Operator (!=)

The Not Equal To (!=) operator checks whether two operands have different values. It works opposite to the equal-to operator.

  • Returns true when the operands are not equal.
  • Frequently used to validate conditions and inputs.

Syntax:

var1 != var2


Output
Var1 = 5
Var2 = 10
Var3 = 5
var1 != var2: true
var1 != var3: false

Greater Than Operator (>)

The Greater Than (>) operator checks whether the left operand is greater than the right operand. 

  • Returns true if the left operand is greater.
  • Useful for comparing numeric values.

Syntax:

var1 > var2


Output
Var1 = 30
Var2 = 20
Var3 = 5
var1 > var2: true
var3 > var1: false

Less Than Operator (<)

The Less Than (<) operator checks whether the left operand is smaller than the right operand.

  • Returns true if the left operand is less than the right operand.
  • Often used in loops and conditional expressions.

Syntax:

var1 < var2


Output
Var1 = 10
Var2 = 20
Var3 = 5
var1 < var2: true
var2 < var3: false

Greater Than or Equal To Operator (>=)

The Greater Than or Equal To (>=) operator checks whether the left operand is either greater than or equal to the right operand.

  • Returns true if the left operand is greater than or equal to the right operand.
  • Useful when equality should also satisfy the condition.

Syntax:

var1 >= var2


Output
Var1 = 20
Var2 = 20
Var3 = 10
var1 >= var2: true
var2 >= var3: true

Less Than or Equal To Operator (<=)

The Less Than or Equal To (<=) operator checks whether the left operand is either less than or equal to the right operand.

  • Returns true if the left operand is less than or equal to the right operand.
  • Commonly used for range checking and loop conditions.

Syntax:

var1 <= var2


Output
Var1 = 10
Var2 = 10
Var3 = 9
var1 <= var2: true
var2 <= var3: false

Example: Program that implements all relational operators in Java for user input:


Output
num1 > num2 is false
num1 < num2 is true
num1 >= num2 is false
num1 <= num2 is true
num1 == num2 is false
num1 != num2 is true

Explanation  This program demonstrates the use of all relational operators in Java. It reads two numbers using the Scanner class and compares them using operators such as >, <, >=, <=, ==, and !=. The results of these comparisons are displayed using System.out.println(). Since relational operators return boolean values (true or false), the program helps users understand how different comparisons work in Java.

Advantages of using relational operators

  • Easy to understand: Relational operators are simple and easy to understand, making it easy to write code that performs comparisons.
  • Versatile: Relational operators can be used to compare values of different data types, such as integers, floating-point numbers, and strings.
  • Essential for making decisions: Relational operators are essential for making decisions in a program, as they allow you to control the flow of a program based on the results of comparisons.
  • Efficient: Relational operators are efficient, as they perform comparisons quickly and accurately.
  • Reusable code: The code that uses relational operators can be reused in different parts of a program, making it easier to maintain and update the code.
  • Improved code readability: By using relational operators, you can make your code more readable and understandable, as the comparisons are clearly stated in the code.
  • Debugging made easier: Relational operators make debugging easier, as you can use them to check the values of variables and to find out where a problem is occurring in your code.
Comment
Article Tags: