In this section, we will learn what comparison (AKA relational) operators are and how they work in Java.
Note: Before reading this section, we’re assuming that you already read the Java operator section.
What are the Relational Operators in Java? (Comparison Operators)
We do use comparison in real life pretty much every day, if not every hour! For example, when we want to buy a phone, we compare multiple brands with each other and the one that attracts us more (price based, design based etc.) We buy that one. Or when we want to buy fruit, we compare their colors, their freshness and choose those that look healthier.
The comparison happens in Java programming as well.
Most of the times we compare the value of a variable or the values of multiple variables with each other and based on the result, decide to run a set of instructions (codes) or to just skip it.
This is where comparison operators can help.
List of Relational Operators in Java
Here’s the list of comparison operators that can be used in Java programming:
Operator | Symbol |
Equal | == |
Not equal | != |
Greater than | > |
Less than | < |
Greater Than or equal to | >= |
Less than or equal to | <= |
Conditional Statements
Comparison operators are mostly used within conditional-statements. For example, if-statement, while-loop, for-loop etc.
Before jumping into these operators and start to explain them, we need to give an introductory explanation about one of these conditional statements so that we can run some example and show you in practical how these operators work.
For this reason, we start with an introduction to the if statement.
Conditional Statements: If statement
Note: the explanation about if-statement in this section will be shallow, but you can learn this statement deeply in the if-statement section.
The meaning that this statement has in real world also applies inside the Java language as well.
For example, in the real world we use if statement to say things like: if my phone rang, I’ll answer. Similarly, in Java we use if statement for checking one or more values and based on the values we decide to run a set of instructions or just skip it.
For example: if the value of a variable that is named height is higher than 100 then the message “The height is higher than 100” should be sent to the screen otherwise the message “The height is less than 100” is the one to be sent to the screen.
This is an example where the if statement can be used.
Alright let’s see the structure of the if-statement:
if (condition){ //body of the if-statement where code is and will be run if the condition is true }else{ //the code here will only run if the condition is not true. }
- if: To create an if statement, we start with the keyword if.
- (): The condition that we want to be checked is put in the parentheses after the if statement.
This is where the comparison operators are useful.
For example, we can use the equal == operator and check the value of a variable with another value and if they were equal then the body of the if statement runs otherwise the body will be skipped.
- {}: the body of an if-statement starts from open brace {and ends at closing brace}. And within this body, we put the codes and instructions that want to be run if the condition of the if-statement is true.
- else: the else keyword comes after the body of the if statement and is used to set another set of instructions that only will run if the condition that is set for the if statement results false. You can think of the instructions set for the else statement as the plan B.
Note: the body of else-statement is within the braces that come after the else keyword. Also this else-statement does not have parentheses to put a new condition.
If statement Example:
public class Simple { public static void main(String[] args) { int age = 20; if (age > 21) { System.out.println("Yes the age is greater than 21."); } else { System.out.println("No the age is less than 21."); } } }
Output: No the age is less than 21.
In this example, there’s a variable named age with the value 20. We then used the if statement to see if the value of this variable is higher than 21 via the Bigger Than > operator. But because the value is less than 21, the result of the condition set in the if statement will return false and the body of the else statement will run instead.
Alright, now that we have a general view on how if-statement works, let’s jump into comparison-operators and explain them one by one:
Note: For the sake of simplicity, this section will not use complex if statements, but as we progress along, we will give more advanced examples of this statement.
Java Equal == Operator:
The equal == operator is used to compare two values and see if they are equal. If the two operands are equal, then the result will be true otherwise false.
Equal == Operator Example:
public class Simple { public static void main(String[] args) { int height = 100; if (height == 100) { System.out.println("Yes the height is equal to 100."); } else { System.out.println("No the height is not equal to 100."); } } }
Output:
Yes the height is equal to 100.
In this example, the purpose of the if statement is to check whether the value of the height variable is equal 100.
Here, because the value of the height variable is actually equal to 100, the condition that is set for the if statement will result true and so the body of the if statement will run.
Note: either the body of the if statement will run or the body of the else statement, but not both. So here, because the body of the if statement ran, the else statement is ignored.
Java Not Equal != Operator:
This operator is on the contrary to the equal == operator. Basically, this operator is used to check whether the two operands involved are not equal. If they weren’t equal, then the result of the comparison is true otherwise false.
Not Equal != Operator Example:
public class Simple { public static void main(String[] args) { int age = 20; if (age !=21){ System.out.println("The age is not 21! It's actually: "+ age); }else{ System.out.println("The age is 21!"); } } }
Output: The age is not 21! It's actually: 20
Because the value of the age variable is not 21, the result of the condition set for the if statement is true and so its body ran.
Note: for both == and != operators, if the involved operands were variables that are storing a reference to objects, these operators will be used to check and see if both variables are pointing to the same object. More on this in later sections.
Example:
public class Simple { public static void main(String[] args) { String str1 = new String("Hello"); System.out.println(str1=="Hello"); System.out.println(str1.equals("Hello")); } }
Output:
False
True
The str1 variable stored the value Hello. But when we used the == operator to check for equality with a string literal that is also Hello we got the value false as the output. This is because the string literal Hello and the object that is referring by the str1 variable are two separate objects.
Java Greater Than > Operator:
This operator is used to see if the value of the operand on the left side of this operator is larger compared to the operand on the right side and if it was, then the result of the comparison is true otherwise the result is false.
Greater Than > Operator Example:
public class Simple { public static void main(String[] args) { int age = 21; if (age > 21){ System.out.println("The value of the age is bigger than 21."); }else{ System.out.println("The value of the age is less than 21."); } } }
Output:
The value of the age is less than 21.
Note: in this example, the value of the age variable is exactly 21 which is equal to the value that this variable is compared with.
But because the Bigger-Than > operator only returns true if the left operand is larger than the right operand, then the result of this condition is false and the body of the else statement is the one to run!
Basically, this operator doesn’t care if the two operands are equal in value.
Java Less Than < Operator:
This operator is on the opposite side of Greater-Than > Operator. Which means this operator checks to see if the operand on the left side is lesser compared to the operand on the right side. If it was, then the result is true, otherwise false.
Less Than < Operator Example:
public class Simple { public static void main(String[] args) { int age = 210; if (age < 21){ System.out.println("The value of the age is less than 21."); }else{ System.out.println("The value of the age is larger than 21."); } } }
Output: The value of the age is larger than 21.
Because the value of the age variable is larger than 21, the result of the age < 21 expression is false and so the body of the else statement is the one to be executed.
Java Greater than or Equal To >= Operator:
This operator is the combination of equal == and Greater-Than > operators.
Basically, via this operator, the operands are checked for two conditions:
- Is the left operand greater than the right operand? If yes, then return true.
- Is the two operands are equal in values? If yes, then return true.
If both of these comparisons resulted false, then in that case, the final result will be false as well.
Greater than or Equal To >= Operator Example:
public class Simple { public static void main(String[] args) { int height = 4; if (height >=5){ System.out.println("The height is more or equal to 5 inches."); }else{ System.out.println("The height is less than 5 inches."); } } }
Output: The height is less than 5 inches.
In this example, the value of the height variable is neither equal to 5 nor higher than 5 and so the result of the condition set in the if statement is false and the body of the else statement is executed instead.
Java Less Than or Equal To <= Operator:
This operator is the combination of equal == and Less-Than < operators.
Basically, via this operator, the operands are checked for two conditions:
- Is the left operand lesser than the right operand? If yes then return true.
- Is the two operands are equal in values? If yes, then return true.
If both of these comparisons resulted false, then in that case, the final result will be false as well.
Less Than or Equal To <= Operator Example:
public class Simple { public static void main(String[] args) { int height = 5; if (height <=5){ System.out.println("The height is less or equal to 5 inches."); }else{ System.out.println("The height is more than 5 inches."); } } }
Output: The height is less or equal to 5 inches.
In this example, the value of the height variable is equal to the value 5 and so the result of the condition of the if statement is true and its body ran.