Table of contents
1.
Introduction:
2.
What are Relational Operators in Java?
2.1.
Syntax:
3.
Types of Relational Operators in Java
3.1.
Equal To Operator(‘==’)
3.2.
Java
3.3.
Not Equal To Operator(‘!=’)
3.4.
Java
3.5.
Greater Than Operator(‘>’)
3.6.
Java
3.7.
Less Than Operator(‘<’)
3.8.
Java
3.9.
Great Than or Equal To Operator(‘>=’)
3.10.
Java
3.11.
Less Than or Equal To Operator(‘<=’)
3.12.
Java
4.
Frequently Asked Questions
4.1.
What are the six relational operators in Java?
4.2.
Can I use <= in Java?
4.3.
What are operators?
4.4.
What are relational operators?
5.
Conclusion
Last Updated: Nov 12, 2024
Easy

Relational Operators in Java

Author NISHANT RANA
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction:

Operators are the most important part of any programming language. Operators can be used to perform various calculations and operations such as logical, arithmetic, relational, etc. In java, we have the following different types of Operators based on their functionalities:-

  1. Assignment Operators
  2. Relational Operators
  3. Arithmetic Operators
  4. Logical Operators
  5. Ternary Operators
  6. Bitwise Operators
  7. Unary Operators
  8. Shift Operators
Relational Operators in Java

What are Relational Operators in Java?

Relational operators in Java are used to compare two values or expressions. They return a boolean result (true or false) based on the comparison. The primary relational operators are:

  • == (equal to)
  • != (not equal to)
  • > (greater than)
  • < (less than)
  • >= (greater than or equal to)
  • <= (less than or equal to)
    These operators are crucial for decision-making and controlling program flow.

Syntax:

variable1 operator variable2

Let us now discuss various types of relational operators and practice them on online java compiler.

Read Also This Topic-  Iteration Statements in Java and Hashcode Method in Java.

Types of Relational Operators in Java

Equal To Operator(‘==’)

This operator checks if the two objects/operands are equal or not. It returns ‘true’ if both the values are equal else it returns false.

Syntax:

Variable1 == Variable2

Sample Code:

  • Java

Java

class Main {
   public static void main(String[] args)
   {
       int variable1 = 3, variable2 = 9, variable3 = 3;

       System.out.println("var1 == var2: "+ (var1 == var2));
       System.out.println("var1 == var3: "+ (var1 == var3));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

variable1 == variable2: false
variable1 == variable3: true

Also see, Swap Function in Java

Not Equal To Operator(‘!=’)

This operator is used to check if the two objects/operands are equal or not. It returns ‘true’ if both the values are not equal else it returns false. It is just opposite to the Equal To operator.

Syntax:

Variable1 != Variable2

 

Sample Code:

  • Java

Java

class Main {


   public static void main(String[] args)
   {
       int variable1 = 3, variable2 = 9, variable3 = 3;

       System.out.println("variable1 != variable2: "+ (variable1 != variable2));
       System.out.println("variable1 != variable3: "+ (variable1 != variable3));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

variable1 != variable2: true
variable1 != variable3: false

Greater Than Operator(‘>’)

This operator is used to check if the left-hand side operand/object is greater than the right-hand side operand/object. It will return true if the left-hand side operand/object is greater than the right-hand side operand/object else false.

Syntax:

Variable1 > Variable2 

Sample Code:

  • Java

Java

class Main {
   public static void main(String[] args)
   {
       int variable1 = 3, variable2 = 9, variable3 = 1;

       System.out.println("variable1 > variable2: "+ (variable1 > variable2));
       System.out.println("variable1 > variable3: "+ (variable1 > variable3));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

variable1 > variable2: false
variable1 > variable3: true

Less Than Operator(‘<’)

This operator is used to check if the left-hand side operand/object is less than the right-hand side operand/object. It will return true if the left-hand side operand/object is less than the right-hand side operand/object else false.

Syntax:

Variable1 < Variable2 

Sample Code:

  • Java

Java

class Main {
   public static void main(String[] args)
   {
       int variable1 = 3, variable2 = 9, variable3 = 1;

       System.out.println("variable1 < variable2: "+ (variable1 < variable2));
       System.out.println("variable1 < variable3: "+ (variable1 < variable3));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

variable1 < variable2: true
variable1 < variable3: false

Great Than or Equal To Operator(‘>=’)

This operator is used to check if the left-hand side operand/object is greater than or equal to the right-hand side operand/object. It will return true if the left-hand side operand/object is greater than or equal to the right-hand side operand/object else false.

Syntax:

Variable1 >= Variable2 

Sample Code:

  • Java

Java

class Main {


   public static void main(String[] args)
   {
       int variable1 = 3, variable2 = 9, variable3 = 3;

       System.out.println("variable1 >= variable2: "+ (variable1 >= variable2));
       System.out.println("variable1 >= variable3: "+ (variable1 >= variable3));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

variable1 >= variable2: false
variable1 >= variable3: true

 

You can also read about the topic of Java Destructor, and Duck Number in Java.

Less Than or Equal To Operator(‘<=’)

This operator is used to check if the left-hand side operand/object is less than or equal to the right-hand side operand/object. It will return true if the left-hand side operand/object is less than or equal to the right-hand side operand/object else false.

Syntax:

Variable1 <= Variable2 

Sample Code:

  • Java

Java

class Main {
   public static void main(String[] args)
   {
       int variable1 = 3, variable2 = 9, variable3 = 3;

       System.out.println("variable1 <= variable2: "+ (variable1 <= variable2));
       System.out.println("variable1 <= variable3: "+ (variable1 <= variable3));
   }
}
You can also try this code with Online Java Compiler
Run Code

Output:

variable1 <= variable2: true
variable1 <= variable3: true

Frequently Asked Questions

What are the six relational operators in Java?

The six relational operators in Java are: == (equal to), != (not equal to), > (greater than), < (less than), >= (greater than or equal to), and <= (less than or equal to).

Can I use <= in Java?

Yes, you can use <= in Java. It is a relational operator that checks if the left operand is less than or equal to the right operand.

What are operators?

These are the special symbols that are used to perform a specific operation on two or more operands and then return the result.

What are relational operators?

Relational Operators are the operators used to compare two or more objects in Java. They return a Boolean value after comparing. They are mainly used in the loops and conditional if/else, switch statements.

Conclusion

In this article, we have extensively discussed the following things:

  1. We first discussed what are operators.
  2. Then we discussed various types of Relational Operators.

Read about Bitwise Operators in C here.

We hope that this blog has helped you enhance your knowledge regarding Relational Operators in the Java language and if you would like to learn more, check out our articles here.

Live masterclass