URL: https://www.progressiverobot.com/relational-operators-in-java/

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

relational operators illustration for: 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](/community/tutorials/java-data-types-primitives-and-binary-literals) 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 &gt; b);
		System.out.println(a &lt; b);
		System.out.println(a &gt;= b);
		System.out.println(a &lt;= b);

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

	}

}

class Data1 {
}
				
			

Output: