Skip to content
Related Articles

Related Articles

Java Relational Operators with Examples
  • Last Updated : 25 Nov, 2019

Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculation and functions be it logical, arithmetic, relational etc. They are classified based on the functionality they provide. Here are a few types:

  1. Arithmetic Operators
  2. Unary Operators
  3. Assignment Operator
  4. Relational Operators
  5. Logical Operators
  6. Ternary Operator
  7. Bitwise Operators
  8. Shift Operators

This article explains all that one needs to know regarding the Relational Operators.

Relational Operators

These are a bunch of binary operators that are used to check for relations between two operands including equality, greater than, less than etc. They return a boolean result after the comparison and are extensively used in looping statements as well as conditional if-else statements and so on. The general format of representing relational operator is:

variable1 relation_operator variable2

Let’s look at each one of the relational operators in Java:



  1. ‘Equal to’ operator (==): This operator is used to check whether the two given operands are equal or not. The operator returns true if the operand at the left-hand side is equal to the right-hand side, else false.
    Syntax:
    var1 == var2

    Example:

    var1 = "GeeksforGeeks"
    var2 = 20
    
    var1 == var2 results in false




    // Java code to illustrate equal to operator
      
    import java.io.*;
      
    class Relational {
        public static void main(String[] args)
        {
            // initializing variables
            int var1 = 5, var2 = 10, var3 = 5;
      
            // Displaying var1, var2, var3
            System.out.println("Var1 = " + var1);
            System.out.println("Var2 = " + var2);
            System.out.println("Var3 = " + var3);
      
            // Comparing var1 and var2
            System.out.println("var1 == var2: "
                               + (var1 == var2));
      
            // Comparing var1 and var3
            System.out.println("var1 == var3: "
                               + (var1 == var3));
        }
    }
    Output:
    Var1 = 5
    Var2 = 10
    Var3 = 5
    var1 == var2: false
    var1 == var3: true
    
  2. ‘Not equal to’ Operator(!=): This operator is used to check whether the two given operands are equal or not. It functions opposite to that of the equal-to operator. It returns true if the operand at the left-hand side is not equal to the right-hand side, else false.
    Syntax:
    var1 != var2

    Example:

    var1 = "GeeksforGeeks"
    var2 = 20
    
    var1 != var2 results in true




    // Java code to illustrate
    // not equal to operator
      
    import java.io.*;
      
    class Relational {
        public static void main(String[] args)
        {
            // initializing variables
            int var1 = 5, var2 = 10, var3 = 5;
      
            // Displaying var1, var2, var3
            System.out.println("Var1 = " + var1);
            System.out.println("Var2 = " + var2);
            System.out.println("Var3 = " + var3);
      
            // Comparing var1 and var2
            System.out.println("var1 == var2: "
                               + (var1 != var2));
      
            // Comparing var1 and var3
            System.out.println("var1 == var3: "
                               + (var1 != var3));
        }
    }
    Output:
    Var1 = 5
    Var2 = 10
    Var3 = 5
    var1 == var2: true
    var1 == var3: false
    
  3. ‘Greater than’ operator(>): This checks whether the first operand is greater than the second operand or not. The operator returns true when the operand at the left-hand side is greater than the right-hand side.
    Syntax:
    var1 > var2

    Example:



    var1 = 30
    var2 = 20
    
    var1 > var2 results in true




    // Java code to illustrate
    // greater than operator
      
    import java.io.*;
      
    class Relational {
        public static void main(String[] args)
        {
            // initializing variables
            int var1 = 30, var2 = 20, var3 = 5;
      
            // Displaying var1, var2, var3
            System.out.println("Var1 = " + var1);
            System.out.println("Var2 = " + var2);
            System.out.println("Var3 = " + var3);
      
            // Comparing var1 and var2
            System.out.println("var1 > var2: "
                               + (var1 > var2));
      
            // Comparing var1 and var3
            System.out.println("var3 > var1: "
                               + (var3 >= var1));
        }
    }
    Output:
    Var1 = 30
    Var2 = 20
    Var3 = 5
    var1 > var2: true
    var3 > var1: false
    
  4. ‘Less than’ Operator(<): This checks whether the first operand is less than the second operand or not. The operator returns true when the operand at the left-hand side is less than the right-hand side. It functions opposite to that of the greater than operator.
    Syntax:
    var1 < var2

    Example:

    var1 = 10
    var2 = 20
    
    var1 < var2 results in true




    // Java code to illustrate
    // less than operator
      
    import java.io.*;
      
    class Relational {
        public static void main(String[] args)
        {
            // initializing variables
            int var1 = 10, var2 = 20, var3 = 5;
      
            // Displaying var1, var2, var3
            System.out.println("Var1 = " + var1);
            System.out.println("Var2 = " + var2);
            System.out.println("Var3 = " + var3);
      
            // Comparing var1 and var2
            System.out.println("var1 < var2: "
                               + (var1 < var2));
      
            // Comparing var2 and var3
            System.out.println("var2 < var3: "
                               + (var2 < var3));
        }
    }
    Output:
    Var1 = 10
    Var2 = 20
    Var3 = 5
    var1 < var2: true
    var2 < var3: false
    
  5. 'Greater than or equal to' operator(>=): This checks whether the first operand is greater than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is greater than or equal to the right-hand side.
    Syntax:
    var1 >= var2

    Example:

    var1 = 20
    var2 = 20
    var3 = 10
    
    var1 >= var2 results in true
    var2 >= var3 results in true




    // Java code to illustrate
    // greater than or equal to operator
      
    import java.io.*;
      
    class Relational {
        public static void main(String[] args)
        {
            // initializing variables
            int var1 = 20, var2 = 20, var3 = 10;
      
            // Displaying var1, var2, var3
            System.out.println("Var1 = " + var1);
            System.out.println("Var2 = " + var2);
            System.out.println("Var3 = " + var3);
      
            // Comparing var1 and var2
            System.out.println("var1 >= var2: "
                               + (var1 >= var2));
      
            // Comparing var2 and var3
            System.out.println("var2 >= var3: "
                               + (var3 >= var1));
        }
    }
    Output:
    Var1 = 20
    Var2 = 20
    Var3 = 10
    var1 >= var2: true
    var2 >= var3: false
    
  6. 'Less than or equal to' Operator(<): This checks whether the first operand is less than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is less than or equal to the right-hand side.
    Syntax:
    var1 <= var2

    Example:

    var1 = 10
    var2 = 10
    var3 = 9
    
    var1 <= var2 results in true
    var2 <= var3 results in false




    // Java code to illustrate
    // less than or equal to operator
      
    import java.io.*;
      
    class Relational {
        public static void main(String[] args)
        {
            // initializing variables
            int var1 = 10, var2 = 10, var3 = 9;
      
            // Displaying var1, var2, var3
            System.out.println("Var1 = " + var1);
            System.out.println("Var2 = " + var2);
            System.out.println("Var3 = " + var3);
      
            // Comparing var1 and var2
            System.out.println("var1 <= var2: "
                               + (var1 <= var2));
      
            // Comparing var2 and var3
            System.out.println("var2 <= var3: "
                               + (var2 <= var3));
        }
    }
    Output:
    Var1 = 10
    Var2 = 10
    Var3 = 9
    var1 <= var2: true
    var2 <= var3: false
    

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :