๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

Relational Operators in Java  


Introduction

  • Relational operators are used to compare two values or expressions and return a boolean value (true or false).
  • Relational operators are explained as below :-
    • == (Equal to Operator):
      • Purpose: Checks if two values are equal.
      • Example:
        int a = 5, b = 5;  
        System.out.println(a == b); // Output: true
    • != (Not equal to operator):
      • Purpose: Checks if two values are not equal.
      • Example:
        int a = 5, b = 10;  
        System.out.println(a != b); // Output: true
    • < (Less Than Operator):
      • Purpose: Checks if the left operand is less than the right operand.
      • Example:
        int a = 5, b = 10;  
        System.out.println(a < b); // Output: true
    • > (Greater Than Operator):
      • Purpose: Checks if the left operand is greater than the right operand.
      • Example:
        int a = 10, b = 5;  
        System.out.println(a > b); // Output: true
    • <= (Less than or equal to operator):
      • Purpose: Checks if the left operand is less than or equal to the right operand.
      • Example:
        int a = 5, b = 10;  
        System.out.println(a <= b); // Output: true
    • >= (Greater than or equal to operator):
      • Purpose: Checks if the left operand is greater than or equal to the right operand.
      • Example:
        int a = 10, b = 10;  
        System.out.println(a >= b); // Output: true
  • Program :
    public class RelationalOperators
    {
        public static void main(String[] args)
        {
            // Initialize variables
            int a = 10, b = 20;
            double x = 15.5, y = 15.5;
    
            // Equal To (==)
            System.out.println("Equal To (a == b): " + (a == b));  // Output: false
            System.out.println("Equal To (x == y): " + (x == y));  // Output: true
    
            // Not Equal To (!=)
            System.out.println("Not Equal To (a != b): " + (a != b));  // Output: true
            System.out.println("Not Equal To (x != y): " + (x != y));  // Output: false
    
            // Less Than (<)
            System.out.println("Less Than (a < b): " + (a < b));  // Output: true
            System.out.println("Less Than (x < y): " + (x < y));  // Output: false
    
            // Greater Than (>)
            System.out.println("Greater Than (a > b): " + (a > b));  // Output: false
            System.out.println("Greater Than (x > y): " + (x > y));  // Output: false
    
            // Less Than or Equal To (<=)
            System.out.println("Less Than or Equal To (a <= b): " + (a <= b));  // Output: true
            System.out.println("Less Than or Equal To (x <= y): " + (x <= y));  // Output: true
    
            // Greater Than or Equal To (>=)
            System.out.println("Greater Than or Equal To (a >= b): " + (a >= b));  // Output: false
            System.out.println("Greater Than or Equal To (x >= y): " + (x >= y));  // Output: true
        }
    }
    Output:
    Equal To (a == b): false
    Equal To (x == y): true
    Not Equal To (a != b): true
    Not Equal To (x != y): false
    Less Than (a < b): true
    Less Than (x < y): false
    Greater Than (a > b): false
    Greater Than (x > y): false
    Less Than or Equal To (a <= b): true
    Less Than or Equal To (x <= y): true
    Greater Than or Equal To (a >= b): false
    Greater Than or Equal To (x >= y): true
Some Advance Important Points :-
  • Comparison of Primitives:
    • Relational operators can be used with all primitive data types except boolean.
    • Example: int a = 10, b = 20; boolean result = a < b; // true.
  • Boolean Result:
    • Relational operators always return a boolean value (true or false).
    • Example: 5 > 3 evaluates to true.
  • Floating-Point Precision Issues:
    • Comparing float or double values may lead to unexpected results due to precision issues.
    • Example: (0.1 + 0.2) == 0.3; // false due to floating-point representation errors.
  • String Comparison:
    • Relational operators (<, >, etc.) cannot be used with String objects; use compareTo for lexicographical comparison.
    • Example: "abc".compareTo("def") < 0 // true.
  • Equality of Objects:
    • == checks reference equality (if two references point to the same object), not content equality.
    • Use .equals() to compare the content of objects.
    • Example:
      String s1 = "Java";
      String s2 = "Java";
      System.out.println(s1 == s2);       // true (same reference in the string pool)
      System.out.println(s1.equals(s2)); // true (content is the same)
  • Not Applicable to Arrays:
    • Relational operators cannot directly compare arrays. Use Arrays.equals() or iterate through elements.
    • Example:
      int[] arr1 = {1, 2, 3};
      int[] arr2 = {1, 2, 3};
      System.out.println(arr1 == arr2); // false (different references)
  • Operator Precedence:
    • Relational operators have higher precedence than logical operators (&&, ||) but lower precedence than arithmetic operators (+, *).
    • Example: 3 + 5 > 7 && 2 < 4; // true.
  • Chaining Not Allowed:
    • You cannot directly chain relational operators like a < b < c.
    • Example: 10 < 20 < 30; // Compilation error.
  • Works with Mixed Types:
    • Relational operators can compare values of different numeric types (int, float, etc.). Implicit casting applies to the smaller type.
    • Example: int a = 5; double b = 5.0; System.out.println(a == b); // true.
  • Cannot Use with null:
    • Relational operators other than == or != cannot be used with null.
    • Example:
      String s = null;
      System.out.println(s == null); // true
      System.out.println(s < "Java"); // Compilation error
  • Comparison of Characters:
    • Relational operators can compare char values based on their Unicode values.
    • Example: 'a' < 'b' // true (Unicode 97 < 98).
  • Usage in Control Structures:
    • Frequently used in if, while, for and switch conditions to determine the flow of execution.
    • Example: if (a >= b) { /* logic */ }.
  • Equality with NaN:
    • For double and float, NaN (Not a Number) is not equal to any value, including itself.
    • Example:
      double value = Double.NaN;
      System.out.println(value == value); // false
  • Efficiency:
    • Relational operators are highly optimized in Java for performance.
  • Interplay with Auto-boxing:
    • Relational operators work on unboxed values when comparing wrapper classes (Integer, Double, etc.).
    • Example:
      Integer a = 100, b = 100;
      System.out.println(a == b); // true (cached values)
  • Behavior with Booleans:
    • Only == and != are valid for boolean values; other relational operators cannot be applied.
    • Example:
      boolean flag = true;
      System.out.println(flag == true); // true
      System.out.println(flag > true); // Compilation error