Comparison Operators in Java

Comparison or Relational Operators in Java

Comparison or Relational operators are used to compare two values and determine if they are equal, greater than, or less than each other.

In Java, there are several comparison operators:

OperatorMeaningExampleDescription
= =Equala == bChecks if a is equal to b
!=Not equala!=bChecks if a is not equal to b
<Less thana<bChecks if a is less than b
>Greater thana>bChecks if a is greater than b
<=Less than or equala<=bChecks if a is less than or equal to b
>=Greater than or equala>=Checks if a is greater than or equal to b
Comparison operators table
  • ==: This operator is used to determine if two values are equal to each other. For example:
int a = 5;
int b = 5;
if (a == b) {
  System.out.println("a and b are equal");
}
  • !=: This operator is used to determine if two values are not equal to each other. For example:
int a = 5;
int b = 6;
if (a != b) {
  System.out.println("a and b are not equal");
}
  • >: This operator is used to determine if one value is greater than another. For example:
int a = 5;
int b = 6;
if (a > b) {
  System.out.println("a is greater than b");
}
  • <: This operator is used to determine if one value is less than another. For example:
int a = 5;
int b = 6;
if (a < b) {
  System.out.println("a is less than b");
}
  • >=: This operator is used to determine if one value is greater than or equal to another. For example:
int a = 5;
int b = 5;
if (a >= b) {
  System.out.println("a is greater than or equal to b");
}
  • <=: This operator is used to determine if one value is less than or equal to another. For example:
int a = 5;
int b = 5;
if (a <= b) {
  System.out.println("a is less than or equal to b");
}

Example:

public class Test {
    public static void main(String[] args) {
        int a = 10;
        int b = 5;
        boolean x = true;
        boolean y = false;
        boolean result;

        // Integer comparisons
        result = (a == b);
        System.out.println(a + " == " + b + " : " + result); // Output: 10 == 5 : false

        result = (a != b);
        System.out.println(a + " != " + b + " : " + result); // Output: 10 != 5 : true

        result = (a > b);
        System.out.println(a + " > " + b + " : " + result);  // Output: 10 > 5 : true

        result = (a < b);
        System.out.println(a + " < " + b + " : " + result);  // Output: 10 < 5 : false

        result = (a >= b);
        System.out.println(a + " >= " + b + " : " + result); // Output: 10 >= 5 : true

        result = (a <= b);
        System.out.println(a + " <= " + b + " : " + result); // Output: 10 <= 5 : false

        // Boolean comparisons
        result = (x == y);
        System.out.println(x + " == " + y + " : " + result); // Output: true == false : false

        result = (x != y);
        System.out.println(x + " != " + y + " : " + result); // Output: true != false : true
    }
}

Output:

10 == 5 : false
10 != 5 : true
10 > 5 : true
10 < 5 : false
10 >= 5 : true
10 <= 5 : false
true == false : false
true != false : true

It is important to note that comparison operators can only be used with certain data types, such as int, double, char, and boolean. They cannot be used with reference types, like String. To compare strings in Java, you can use the .equals() method. For example:

String str1 = "Hello";
String str2 = "Hello";
if (str1.equals(str2)) {
  System.out.println("str1 and str2 are equal");
}

Comparison operators are a fundamental part of programming and are used in many different contexts, including decision making and looping. Understanding how to use these operators is an essential skill for any Java programmer.

Frequently Asked Questions

What are comparison or relational operators in Java?

Comparison operators, also known as relational operators, are used to compare two expressions. They return a boolean value, either true or false, depending on the result of the comparison.

How many comparison operators are there in Java?

There are six comparison opertors in Java. Which are: == (Equal to), != (Not equal to), > (Greater than), < (Less than), >= (Greater than or equal to), <= (Less than or equal to).

Can I use comparison operators to compare objects in Java?

Yes, you can use comparison operators to compare objects in Java. However, it is important to note that the equality operator (==) will compare the object references, not the content of the objects. This means that if two object references point to the same object in memory, the equality operator will return true, even if the objects themselves are not equal.

To compare objects based on their content, you should use the equals() method. The equals() method is defined in the Object class, and all Java objects inherit this method. You can override the equals() method in your custom classes to define the comparison logic based on the content of the objects.

How to compare strings in Java using relational operators?

You can use the equals() method to compare the content of two strings. For example: String str1 = "Hello"; String str2 = "World"; boolean areEqual = str1.equals(str2); will return false.