Tutorial

Relational Operators in Java

Published on August 3, 2022
Default avatar

By Pankaj

Relational Operators in Java

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Relational Operators in Java are used to comparing two variables for equality, non-equality, greater than, less than, etc. Java relational operator always returns a boolean value - true or false.

Relational Operators in Java

Java has 6 relational operators.

  1. == is the equality operator. This returns true if both the operands are referring to the same object, otherwise false.
  2. != is for non-equality operator. It returns true if both the operands are referring to the different objects, otherwise false.
  3. < is less than operator.
  4. > is greater than operator.
  5. <= is less than or equal to operator.
  6. >= is greater than or equal to operator.

Relational Operators Supported Data Types

  • The == and != operators can be used with any primitive data types as well as objects.
  • The <, >, <=, and >= can be used with primitive data types that can be represented in numbers. It will work with char, byte, short, int, etc. but not with boolean. These operators are not supported for objects.

Relational Operators Example

package com.journaldev.java;

public class RelationalOperators {

	public static void main(String[] args) {

		int a = 10;
		int b = 20;

		System.out.println(a == b);
		System.out.println(a != b);
		System.out.println(a > b);
		System.out.println(a < b);
		System.out.println(a >= b);
		System.out.println(a <= b);

		// objects support == and != operators
		System.out.println(new Data1() == new Data1());
		System.out.println(new Data1() != new Data1());

	}

}

class Data1 {
}

Output:

Relational Operators Java Example
Relational Operators Java Example

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
April 25, 2020

Hello Thanks for sharing the post. I am not able to understand how == works for primitive data type but not for objects. In the above post it was mentioned that it returns true if both the operandi refers to the same object. I understood that it dosen’t work for objects as when we use new keyword new objects are created. So it returns false. But can you please explain how does it work for primitive data types? Thanks

- Mannam

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    March 16, 2020

    equals is an Override method of base class Object, then equals is better for object comparison and not for primitive types

    - Reynier Ramos Portieles

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      August 3, 2019

      Thank you for describing different operators in java, I’m adam from Sweden. I’m 47 years old and want to program in java. Some times the operator == does not work and I have to use .equals() ex in a nestled for-loop. https://pastebin.com/TXbWufVu

      - Carl-Adam Bergund

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Get our biweekly newsletter

        Sign up for Infrastructure as a Newsletter.

        Hollie's Hub for Good

        Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

        Become a contributor

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        Welcome to the developer cloud

        DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

        Learn more
        DigitalOcean Cloud Control Panel