Thursday, December 26, 2024
Google search engine
HomeLanguagesJavaJava Relational Operators with Examples

Java Relational Operators with Examples

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 calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide.

Types of Operators: 

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

Java Relational Operators are a bunch of binary operators 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: 

Syntax:

variable1 relation_operator variable2

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

Operator 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

Illustration:

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

Example:

Java




// Java Program to Illustrate equal to Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    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 and
        // printing corresponding boolean value
        System.out.println("var1 == var2: "
                           + (var1 == var2));
 
        // Comparing var1 and var3 and
        // printing corresponding boolean value
        System.out.println("var1 == var3: "
                           + (var1 == var3));
    }
}


Output

Var1 = 5
Var2 = 10
Var3 = 5
var1 == var2: false
var1 == var3: true

Operator 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

Illustration: 

var1 = "GeeksforGeeks"
var2 = 20

var1 != var2 results in true

Example:

Java




// Java Program to Illustrate No- equal-to Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    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 and
        // printing corresponding boolean value
        System.out.println("var1 != var2: "
                           + (var1 != var2));
 
        // Comparing var1 and var3 and
        // printing corresponding boolean value
        System.out.println("var1 != var3: "
                           + (var1 != var3));
    }
}


Output

Var1 = 5
Var2 = 10
Var3 = 5
var1 != var2: true
var1 != var3: false

Operator 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

Illustration: 

var1 = 30
var2 = 20

var1 > var2 results in true

Example:

Java




// Java code to Illustrate Greater than operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    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 and
        // printing corresponding boolean value
        System.out.println("var1 > var2: " + (var1 > var2));
 
        // Comparing var1 and var3 and
        // printing corresponding boolean value
        System.out.println("var3 > var1: "
                           + (var3 >= var1));
    }
}


Output

Var1 = 30
Var2 = 20
Var3 = 5
var1 > var2: true
var3 > var1: false

Operator 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

Illustration: 

var1 = 10
var2 = 20

var1 < var2 results in true

Example:

Java




// Java code to Illustrate Less than Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    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 and
        // printing corresponding boolean value
        System.out.println("var1 < var2: " + (var1 < var2));
 
        // Comparing var2 and var3 and
        // printing corresponding boolean value
        System.out.println("var2 < var3: " + (var2 < var3));
    }
}


Output

Var1 = 10
Var2 = 20
Var3 = 5
var1 < var2: true
var2 < var3: false

Operator 5: Greater than or equal to (>=)

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

Illustration:

var1 = 20
var2 = 20
var3 = 10

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

Example:

Java




// Java Program to Illustrate Greater than or equal to
// Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    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 and
        // printing corresponding boolean value
        System.out.println("var1 >= var2: "
                           + (var1 >= var2));
 
        // Comparing var2 and var3 and
        // printing corresponding boolean value
        System.out.println("var2 >= var3: "
                           + (var2 >= var3));
    }
}


Output

Var1 = 20
Var2 = 20
Var3 = 10
var1 >= var2: true
var2 >= var3: true

Operator 6: Less than or equal to (<=)

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

Illustration: 

var1 = 10
var2 = 10
var3 = 9

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

Example:

Java




// Java Program to Illustrate Less
// than or equal to operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    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 and
        // printing corresponding boolean value
        System.out.println("var1 <= var2: "
                           + (var1 <= var2));
 
        // Comparing var2 and var3 and
        // printing corresponding boolean value
        System.out.println("var2 <= var3: "
                           + (var2 <= var3));
    }
}


Output

Var1 = 10
Var2 = 10
Var3 = 9
var1 <= var2: true
var2 <= var3: false

program that implements all relational operators in Java for user input:

Java




import java.util.Scanner;
 
public class RelationalOperators {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
     
    //System.out.println("Enter first number: ");
   // int num1 = scan.nextInt();
     
   // System.out.println("Enter second number: ");
   // int num2 = scan.nextInt();
     
    int num1 =1;
    int num2 = 2;
     
     
    System.out.println("num1 > num2 is " + (num1 > num2));
    System.out.println("num1 < num2 is " + (num1 < num2));
    System.out.println("num1 >= num2 is " + (num1 >= num2));
    System.out.println("num1 <= num2 is " + (num1 <= num2));
    System.out.println("num1 == num2 is " + (num1 == num2));
    System.out.println("num1 != num2 is " + (num1 != num2));
  }
}


Output

num1 > num2 is false
num1 < num2 is true
num1 >= num2 is false
num1 <= num2 is true
num1 == num2 is false
num1 != num2 is true

Explanation 

 The code above implements all relational operators in Java for user input. The following is an explanation of the code in detail:

  • Importing the Scanner class: The code starts by importing the Scanner class, which is used to read input from the user. The Scanner class is part of the java.util package, so it needs to be imported in order to be used.
  • Defining the main method: The program then defines the main method, which is the starting point of the program. This is where the program logic is executed.
  • Reading user input: The code uses the Scanner object to read two integers from the user. The first number is stored in the num1 variable, and the second number is stored in the num2 variable.
  • Using relational operators: The code then uses the relational operators >, <, >=, <=, ==, and != to compare the values of num1 and num2. The results of these comparisons are stored in boolean variables, which are then displayed to the user.
  • Displaying results: The results of the comparisons are displayed to the user using the System.out.println method. This method is used to print a message to the console.
  • Closing the Scanner object: Finally, the code closes the Scanner object to prevent memory leaks and to ensure that the program terminates cleanly.

The relational operators in Java return a boolean value of true or false, depending on the result of the comparison. For example, num1 > num2 returns true if num1 is greater than num2, and false otherwise. Similarly, num1 == num2 returns true if num1 is equal to num2, and false otherwise.

Advantages 

There are several advantages of using relational operators in Java, including:

  1. Easy to understand: Relational operators are simple and easy to understand, making it easy to write code that performs comparisons.
  2. Versatile: Relational operators can be used to compare values of different data types, such as integers, floating-point numbers, and strings.
  3. Essential for making decisions: Relational operators are essential for making decisions in a program, as they allow you to control the flow of a program based on the results of comparisons.
  4. Efficient: Relational operators are efficient, as they perform comparisons quickly and accurately.
  5. Reusable code: The code that uses relational operators can be reused in different parts of a program, making it easier to maintain and update the code.
  6. Improved code readability: By using relational operators, you can make your code more readable and understandable, as the comparisons are clearly stated in the code.
  7. Debugging made easier: Relational operators make debugging easier, as you can use them to check the values of variables and to find out where a problem is occurring in your code.
RELATED ARTICLES

Most Popular

Recent Comments