Relational Operators in Java with Example | Types of Java Relational Operators with Sample Program

Are you wondering what are Relational operators in Java? How to use them in your java code? Then, here are the detailed answers to your queries on Relational Operators in Java. Mostly, they are used either in If condition or Loops. Also, you can check the relationship between the two variables using these Java Relational Operators. So, grab the opportunity of the direct links available below and learn completely & efficiently.

This Java Relational Operators Tutorial Includes:

Java Operators

Operators are special symbols that perform specific operations on one, two, or three operands, and then return a result. There are different types of operators are available in java which are given below:

  1. Arithmetic Operators
  2. Unary Operators
  3. Assignment Operators
  4. Relational Operators
  5. Logical Operators
  6. BitWise Operators
  7. Ternary Operators
  8. InstanceOf Operators

Relational Operators in Java

In the last tutorial, we have learned about arithmetic operators, unary operators, and assignment operators. We have also learned the example of these operators. In this tutorial, we will learn what is relational operators in java. The Relational Operators are used to check the relations between the two operands. Relational operators are also called comparison operators because it is used to make a comparison between the two operands. The result of relational operators is always a boolean value. The relational operators are used in the if-else statement and the various loop statements.

Do Refer: 

The following table shows you different relational operators used in Java.

Relational Operators in Java with Example 1

1. Equal to(==) operator: This operator is used to check whether the two operands are equal or not. It returns true if both the operands are equal.

2. Not Equal to(!=) operator: This operator is used to check whether the two operands are equal or not. It returns true if the left-hand side operand is not equal to the right-hand side operand, otherwise, it returns false.

3. >(Greater than) operator: This operator is used to check the first operand is greater than the second operand or not. It returns true if the first operand is greater than the second operand.

4. >=(Greater than equal to) operator: This operator is used to check whether the first operand is greater than or equal to the second operand or not. It returns true if the first operand is greater than or equal to the second operand.

5. <(less than) operator: This operator is used to check the first operand is less than the second operand or not. It returns true if the first operand is less than the second operand.

6. <=(less than equal to): This operator is used to check whether the first operand is less than or equal to the second operand or not. It returns true if the first operand is less than or equal to the second operand.

Relational Operators Supported Data Types

  • The <, >, <=, and >= can be utilized with primitive data types that can be expressed in numbers. It works with char, byte, short, int, etc. but not with boolean. These operators are not supported for objects.
  • The == and != operators can be used with any primitive data types as well as objects.

Java Relational Operators Example

class RelationalOperator{

public static void main(String[] args){

int value1 = 10;
int value2 = 20;

System.out.println("value1 == value2 --> " +(value1 == value2));
System.out.println("value1 != value2 --> " +(value1 != value2));
System.out.println("value1 > value2 --> " +(value1 > value2));
System.out.println("value1 >= value2 --> " +(value1 >= value2));
System.out.println("value1 < value2 --> " +(value1 < value2));
System.out.println("value1 <= value2 --> " +(value1 <= value2));
}
}

Output:

Relational Operators in Java with Example 2