Summary: in this tutorial, you will learn how to use the Java relational operators to compare two values and determine the relationship between them.
Introduction to the Java relational operators
In Java, relational operators are used to compare two values (or variables) and determine the relationship between them. The relational operators are also known as comparison operators.
Java has six relational operators as shown in the following table:
Relational operator | Meaning | Integer, floating-point | Character | Boolean |
---|---|---|---|---|
> | Greater than | Yes | Yes | N/A |
>= | Greater than or equal | Yes | Yes | N/A |
< | Less than | Yes | Yes | N/A |
<= | Less than or equal | Yes | Yes | N/A |
== | Equal to | Yes | Yes | Yes |
!= | Not equal to | Yes | Yes | Yes |
The relational operators always return a boolean value, which can be true or false.
Greater than (>)
The greater than (>) returns true if the left operand is greater than the right operand or false otherwise:
x > y
Code language: Java (java)
The greater than operator can be applied to integers, floating-point numbers, and characters. It is not relevant to the boolean type.
The following example illustrates how to use the greater than operator to integers:
public class App {
public static void main(String[] args) {
int x = 10;
int y = 20;
boolean result = x > y;
System.out.println(result); // false
result = y > x;
System.out.println(result); // true
}
}
Code language: Java (java)
Output:
false
true
Code language: Java (java)
In this example, the comparison x > y returns false because 10 is less than 20 and y > x returns true because 20 is greater than 10.
Greater than or equal to (>=)
The greater than or equal to operator returns true if the left operand is greater than or equal to the right operand or false otherwise.
x >= y
Code language: Java (java)
The greater than operator can be applied to integers, floating-point numbers, and characters. It is not relevant to the boolean type.
The following example illustrates how to use the greater than or equal to operator with floating-point numbers:
public class App {
public static void main(String[] args) {
float x = 10.0f;
float y = 20.0f;
float z = 20.0f;
boolean result = x >= y;
System.out.println(result); // false
result = y >= x;
System.out.println(result); // true
result = z >= y;
System.out.println(result); // true
}
}
Code language: Java (java)
Output:
false
true
true
Code language: Java (java)
In this example, the comparison x >= y returns false because 10.0f
is less than 20.0f
, y >= x returns true because 20.0f
is greater than 10.0f
, and z >= y returns true because 20.0f
is equal to 20.f
.
Less than (<)
The less than operator returns true if the left operand is less than the right operand or false otherwise:
x < y
Code language: Java (java)
The less than operator can be applied to integers, floating-point numbers, and characters. It is not relevant to the boolean type.
The following example shows how to use the less than operator to compare two variables of type character:
public class App {
public static void main(String[] args) {
char ch1 = 'a';
char ch2 = 'b';
char ch3 = 'c';
boolean result = ch1 < ch2;
System.out.println(result); // true
result = ch3 < ch2;
System.out.println(result); // false
}
}
Code language: Java (java)
In this example:
- The Unicode value of ‘a’ is less than the Unicode value of ‘b’, the comparison ch1 < ch2 evaluates to true.
- The Unicode value of ‘c’ is greater than the Unicode value of ‘b’, the comparison ch3 < ch2 evaluates to false.
Less than or equal to (<=)
The less than or equal to operator returns true if the left operand is less than or equal to the right operand or false otherwise:
x <= y
Code language: Java (java)
The less than or equal to operator can be applied to integers, floating-point numbers, and characters. It is not relevant to the boolean type.
The following example illustrates how to use the less than or equal to operator with integer and float values:
public class App {
public static void main(String[] args) {
int x = 10;
double y = 20.0;
int z = 20;
boolean result = x <= y;
System.out.println(result); // true
result = y <= x;
System.out.println(result); // false
result = z <= y;
System.out.println(result); // true
}
}
Code language: Java (java)
Equal to (==)
The equal to operator returns true of the left operand is equal to the right operand or false otherwise:
x == y
Code language: Java (java)
The equal to operator can be applied to all primitive data types.
The following example shows how to use the equal to operator to compare two boolean variables:
public class App {
public static void main(String[] args) {
boolean status = true;
boolean isCompleted = true;
boolean result = status == isCompleted;
System.out.println(result); // true
}
}
Code language: Java (java)
Output:
true
Code language: Java (java)
Not equal to (!=)
The not equal to operator returns true if the left operand is not equal to the right operand or false otherwise:
x != y
Code language: Java (java)
The not equal to operator can be applied to all primitive data types.
The following example shows how to use the not equal to operator with the boolean values:
public class App {
public static void main(String[] args) {
boolean status = true;
boolean isCompleted = true;
boolean result = status != isCompleted;
System.out.println(result); // false
}
}
Code language: Java (java)
In this example, the comparison status != isCompleted
returns false as expected because both variables are true.
Summary
- Java relational operators compare two values and return a boolean value that determines the relationship between them.