Relational Operators in Java Explained [Practical Examples]


JAVA

Introduction to Relational Operators in Java

In Java, relational operators are binary operators that compare the value of two operands. In other words, Java's relational operators check for an equality, greater than, and less than between two operands. These operators return a boolean value to indicate the results of comparison. These operators are widely used in the looping statements and the conditional statements.

Java supports the following relational operators.

  • Equal to
  • Not Equal to
  • Greater than
  • Greater than or equal to
  • Less than
  • Less than or equal to

The generalized syntax to use all the relational operator is as shown below:

 left_operand relational_operator right_operand 

 

Different types of Relational Operators in Java

1. Equal to operator (==)

In Java, equal to operator returns a true if the value of the left operand is same as the right operand, false otherwise. We can use an equal to operator for comparing variable of any primitive data type. Moreover, it also works with the class objects and the String objects.

The statement below demonstrates the use of == operator with the operands.


left_operand == right_operand

Example :


public class Main {
    public static void main(String[] args) {
        // Initializing
        String x = "Hello";
        String y = "Hello";
        int a=528;
        int b=678;
		
        // Using equal to operator String comparison
        System.out.println("String x="+x+" and y="+y+" are same - "+(x==y));
		
        // Using equal to operator integer comparison
        System.out.println("Value of variables a="+a+" and b="+b+" are same - "+(a==b));
		
        // Creating and comparing objects of class 
	Main m1=new Main();
        Main m2=new Main();
        System.out.println("Is this objects equal? "+(m1==m2));
		
        // Copying the object reference and then comparing them
	m1=m2;
	System.out.println("Is this objects equal? "+(m1==m2));
    }
}

Output


String x=Hello and y=Hello are same - true
Value of variables a=528 and b=678 are same - false
Is this objects equal? false
Is this objects equal? true

 

2. Not Equal to operator (!=)

In Java, not equal to operator returns a true if the value of the left operand is not same as the right operand, false otherwise. We can use this operator for comparing variables of any primitive data type. Moreover, it also works with the class objects and String objects.

The statement below demonstrates the use of != operator with the operands.

 left_operand != right_operand 

Example :


public class Main {
    public static void main(String[] args) 
    {
        // Initializing
	String x = "Hello";
        String y = "Hello";
        int a=528;
        int b=678;
		
        // Using not equal to operator String comparison
        System.out.println("String x="+x+" and y="+y+" are different - "+(x!=y));
		
        // Using not equal to operator integer comparison
        System.out.println("Value of variables a="+a+" and b="+b+" are different - "+(a!=b));
		
        // Creating and comparing objects of class 
	Main m1=new Main();
        Main m2=new Main();
        System.out.println("Are this objects different? "+(m1!=m2));
		
        // Copying the object reference and then comparing them
	m1=m2;
	System.out.println("Are this objects different? "+(m1!=m2));
    }
}

Output


String x=Hello and y=Hello are different - false
Value of variables a=528 and b=678 are different - true
Are this objects different? true
Are this objects different? false

 

3. Greater than operator (>)

In Java, greater than operator returns a true if the value of the left operand is greater than the value of the right operand, false otherwise. It will work with some of the primitive data types like char, byte, short, int, float, etc. However, it doesn't work with boolean type and objects.

The statement below demonstrates the use of > operator with the operands.


left_operand > right_operand

Example :


public class Main {
    public static void main(String[] args) {
        char x = 'a';
        char y = 'A';
        double a=5.28927;
        double b=5.28925;
		
        // Using greater than operator for char comparison
        System.out.println("Is char x="+x+" greater than y="+y+" ? "+(x>y));
		
        // Using greater than operator for double comparison
        System.out.println("Is variables a="+a+" greater than b="+b+" ? "+(a>b));
		
    }
}

Output


Is char x=a greater than y=A ? true
Is variables a=5.28927 greater than b=5.28925 ? true

 

4. Greater than equal to operator (>=)

In Java, greater than equal to operator returns a true if the value of the left operand is either greater than or equal to the value of the right operand, false otherwise. It will work with some of the primitive data types like char, byte, short, int, float, etc. However, it doesn't work with boolean type and objects.

The statement below demonstrates the use of >= operator with the operands.


left_operand >= right_operand

Example :


public class Main {
    public static void main(String[] args) {
	// Initializing
        char x = 'a';
        char y = 'A';
        double a=5.2892;
        double b=5.28925;
		
	// Using greater than equal to operator for char comparison
        System.out.println("Is char x="+x+" greater than or equal to y="+y+" ? "+(x>=y));
		
	// Using greater than equal to operator for double comparison
        System.out.println("Is variables a="+a+" greater than or equal to b="+b+" ? "+(a>=b));
		
    }
}

Output


Is char x=a greater than or equal to  y=A ? true
Is variables a=5.2892 greater than or equal to b=5.28925 ? false

 

5. Less than operator (<)

In Java, less than operator returns a true if the value of the left operand is lesser than the value of the right operand, false otherwise. It will work with some of the primitive data types like char, byte, short, int, float, etc. However, it doesn't work with boolean type and objects.

The statement below demonstrates the use of < operator with the operands.

 left_operand < right_operand 

Example :


public class Main {
    public static void main(String[] args) {
        // Initializing
	char x = 'a';
        char y = 'A';
        double a=5.28927;
        double b=5.28925;
		
	// Using less than operator for char comparison
        System.out.println("Is char x="+x+" less than y="+y+" ? "+(x<y));
		
	// Using less than operator for double comparison
        System.out.println("Is variables a="+a+" less than b="+b+" ? "+(a<b));
    }
}

Output


Is char x=a less than y=A ? false
Is variables a=5.28927 less than b=5.28925 ? false

 

6. Less than equal to operator (<=)

In Java, less than equal to operator returns a true if the value of the left operand is either less than or equal to the value of the right operand, false otherwise. It will work with some of the primitive data types like char, byte, short, int, float, etc. However, it doesn't work with boolean type and objects.

The statement below demonstrates the use of <= operator with the operands.

 left_operand <= right_operand 

Example :


public class Main {
    public static void main(String[] args) 
   {
	// Initializing
        char x = 'a';
        char y = 'A';
        double a=5.2892;
        double b=5.28925;
		
        // Using less than equal to operator for char comparison
        System.out.println("Is char x="+x+" less than or equal to y="+y+" ? "+(x<=y));
		
        // Using less than equal to operator for double comparison
        System.out.println("Is variables a="+a+" less than or equal to b="+b+" ? "+(a<=b));		
    }
}

Output


Is char x=a less than or equal to than y=A ? false
Is variables a=5.2892 less than or equal to b=5.28925 ? true

 

Summary

The knowledge of relational operators is a key to start building the logic in Java. Relational operators are extensively used in looping and control statements. In this tutorial, we covered all relational operators supported in Java. We learned in detail about the syntax and how we can use this operators with different data types and objects with example. All in all, this tutorial, covers everything that you need to know in order to have a solid command over relational operators in Java.

 

Further Reading

Java Operators
More about Java Operators

 

Deepak Prasad

Deepak Prasad

Deepak Prasad is the founder of GoLinuxCloud, bringing over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, Networking, and Security. His extensive experience spans development, DevOps, networking, and security, ensuring robust and efficient solutions for diverse projects.

Certifications and Credentials:

  • Certified Kubernetes Application Developer (CKAD)
  • Go Developer Certification
  • Linux Foundation Certified System Administrator (LFCS)
  • Certified Ethical Hacker (CEH)
  • Python Institute PCAP (Certified Associate in Python Programming)
You can connect with him on his LinkedIn profile and join his Facebook and LinkedIn page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment