Relational Operators in Java

Relational Operators : -In this Java Tutorial, we shall learn about Relational Operators in Java with an example program.

What is an Operator in Java?

Operators that give information regarding the relation between two entities/operands are called Relational Operators. In Java, output of relational-operators is boolean value. Java supports following Relational Operators :

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

Relational Operators in Java

The following table gives more information about these relational operators in java.

OperatorSymbolExpressionABExpression with valuesResult(boolean)
Equal to==A==B141414==14true
Not equal to!=A!=B141114!=11true
Greater than>A>B141214>12true
Less than<A<B256325<63true
Greater than or equal to>=A>=B252525>=25true
Less than or equal to<=A<=B252525<=25true

Example 1 – Relational Operators

Let us see an example program, demonstrating the working of relational operators.

Example.java

package com.tutorialkart;
/**
 * @author tutorialkart.com
 */
public class Example {
	public static void main(String[] args) {
		int A=12,B=10;
		System.out.println("A==B  : "+A+"=="+B+" : "+(A==B));
		System.out.println("A!=B  : "+A+"!="+B+" : "+(A!=B));
		System.out.println("A>B   :  "+A+">"+B+" : "+(A>B));
		System.out.println("A<B   :  "+A+"<"+B+" : "+(A<B));
		System.out.println("A>=B  : "+A+">="+B+" : "+(A>=B));
		System.out.println("A<=B  : "+A+"<="+B+" : "+(A<=B));
	}
}

Output

A==B  : 12==10 : false
A!=B  : 12!=10 : true
A>B   :  12>10 : true
A<B   :  12<10 : false
A>=B  : 12>=10 : true
A<=B  : 12<=10 : false

Conclusion

In this Java Programming Tutorial, we have learnt Relational Operators that Java Programming Language provides. In our next tutorial, we shall learn Java Decision Making Statements.