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
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
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
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
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
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
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:
- We first discussed what are operators.
- 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.