JavaJava Comparison Operators

Java Comparison Operators

Java Developer Tutorials

In Java, comparison operators are used to compare two values in order to run a code block or set a variable’s value based on the result. Hence, they help us to make decisions. In order to do so, the return value of a comparison must be a boolean value of either true or false. This programming tutorial will cover Java’s comparison operators as well as how to combine them to evaluate both simple and complex expressions.

Read: Best Online Courses to Learn Java

What Are Java’s Relational Operators

In Java, operators that perform comparisons of two operands are called Relational operators. There are a total of six of them:

  • == : Compares two operands for equality, e.g., x == y.
  • != : Compares two operands for inequality, e.g., x != y.
  • > : Checks if one operand is greater than the other, e.g., x > y.
  • <: Checks if one operand is less than the other, e.g., x < y.
  • >=: Checks if one operand is greater than or equal to the other, e.g., x >= y.
  • <= : Checks if one operand is less than or equal to the other, e.g., x <= y.

Supported Data Types for Relational Operators

The == and != operators can be used with any primitive data types as well as objects.
Meanwhile, the <, >, <=, and >= can be used with any primitive numeric data types (or that can be represented as numbers). Hence, they will work with char, byte, short, int, etc., but not with booleans or objects.

Examples of Primitive Data Type and Object Comparisons in Java

Here is some example code showing a class that compares both primitive data types and objects and prints their results to the console in Java:

public class RelationalOperatorExample {

  public static void main(String args[]) {
    int a = 10, b = 5;
    Integer aa = new Integer(a);
    Integer bb = new Integer(b);

    System.out.println("a > b : " + (a > b)); // true
    System.out.println("a < b : " + (a < b)); // false
    System.out.println("a >= b : " + (a >= b)); // true
    System.out.println("a <= b : " + (a <= b)); // false
    System.out.println("a == b : " + (a == b)); // false
    System.out.println("a != b : " + (a != b)); // true
		
    // objects support == and != operators
    System.out.println(aa == bb); // false
    System.out.println(aa != bb); // true
  }
}

In the case of objects, the == returns true only if the two objects occupy the same space in memory, and thus reference the same object.

Read: Java Math Operators

Combining Relational Operators with Conditional Operators in Java

Relational operators may be combined with Conditional operators in order to chain multiple comparisons together. Relational operators check the condition and decide the desired result on the basis of both conditions. There are three types of conditional operators in Java:

  • && – Conditional or Logical AND
  • || – Conditional or Logical OR
  • ?: – Ternary Operator

Conditional AND Operator in Java

The Conditional AND operator is applied between two boolean expressions. It is known as a short-circuiting operator because it only evaluates the second expression if the first one is true. It then returns true if both expressions are true; otherwise, it returns false.

Conditional OR Operator in Java

Like the Conditional AND, the Conditional OR operator is also applied between two boolean expressions. It returns true if either, or both, of the expressions are true; otherwise, it returns false.

Ternary Operator in Java

The ternary operator is a bit of an oddball in that it consists of three operands. It is basically a short-form of the if-else statement that sets a variable’s value. Many developers find that it makes the code more readable and succinct. The ternary operator’s full syntax in Java is:

result = (condition) ? expression1 : expression2  

Hence, it would replace an IF statement such as this one:

int a = 10, b = 5, c;
if (a == 10) {
  // c is set to 20 because the value of a is 10
  c = 20;
} else {
  c = 40;
}

with this:

int a = 10, b = 5, c;

c = (a == 10) ? 20 : 40;
// Displays 20 because the value of a is 10
System.out.println("Value of c is: " + c);

Here is an example program that uses all three Conditional operators in Java:

public class ConditionalOperatorExample {  
  public static void main(String args[]) {  
    int x=5, y=4, z=7;  
    
    System.out.println(x>y && x>z || y<z); // true  
    System.out.println((x<z || y>z) && x<y);       // false
    
    int result = (x == 4) ? 99: 100;  
    System.out.println("Value of result is: " + result);    // 100
    result = (x <= 5) ? 10: 0;  
    System.out.println("Value of result is: " + result);    // 10
  }  
}  

Final Thoughts on Java Comparison Operators

In this programming tutorial we learned how to evaluate expressions using Java’s Relational and Conditional comparison operators. While best applied to numeric primitive data types, the equals (==) and not equals (!=) operators can be employed to ascertain whether or not two variables point to the same object instance. For more complex String and Object comparisons, Java provides many specialized methods. We will get to those in a future article.

Read more Java programming tutorials and software development tips.

Latest Posts

Related Stories