Relational and Equality Operators in Java

Java Comparison Operators

Equality and relational operators in Java are called comparison operators. There are two equality operators: equals to (==) and not equals to (!=) they test if two values are equal or not. Whereas, relational operators used with ordered types (numbers and characters) to test for greater than and less than relationships. All comparison operators are binary operators and return a boolean result (true or false).

C and C++ programmers may note that in C and C++ 1 is returned as true and zero as false, but in Java there is a separate boolean type for decision making expressions and all comparison operators yield a boolean result (true and false are boolean values in Java).

The following table demonstrates Java's comparison (equality and relational) operators. For demonstration, suppose X and Y are two integer variables containing values 20 and 30 respectively.

Table 1: Demonstration: Java equality and relational operators
Relational Operator X = 20, Y = 30 Result
== (Equal to) X == Y false
!= (Not equal to) X != Y true
> (Greater than) X > Y false
< (Less than) X < Y true
>= (Greater than or equal to) X >= Y false
<= (Less than or equal to) X <= Y true

Java Equality Operators

The == and != are equality operators of Java. The == operator returns true if its two operands are equal and false otherwise. When == operator is applied on primitive operands, it tests whether the operand values themselves are identical. For operands of reference types, however, it tests whether the operands refer to the same object or array. In other words, while comparing two objects == operator compares two memory locations, and of course, if two references point to a single object then memory location will be the same. Also, you cannot test two distinct strings for equality with this operator.

On the contrary, the != operator is exactly the opposite of the == operator. It evaluates to true if its two primitive operands have different values or if its two reference operands refer to different objects or arrays. Otherwise, it evaluates to false.

Note that equality operators can be used with boolean values, objects, or arrays but relational operators cannot be used with boolean values, objects, or arrays. Relational operators can only be used with numbers and characters.

Java Relational Operators

The relational operators operate upon ordered types (numbers and characters) to test for greater than (>), greater than or equal to (>=), less than (<), and less than or equal to (<=) relationships. Relational operators cannot be used with boolean values, objects, or arrays. Relational operators can only be used with numbers and characters.

Last Word

In this tutorial we discussed Java's equality and relational operators they are collectively called comparison operators. Hope you have enjoyed reading this tutorial. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!


Get Free Tutorials by Email

About the Author

is the main author for He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

Plans drawn up for world's tallest wooden skyscraperPosted on Monday February 19, 2018

The 70-storey tower would be 90% wood and have trees and other foliage on every level.

Probe after 'drone made helicopter crash'Posted on Monday February 19, 2018

Investigations are launched after a helicopter reportedly crashed after swerving to avoid a consumer drone.

Dark web paedophile Matthew Falder jailed for 32 yearsPosted on Monday February 19, 2018

Matthew Falder blackmailed victims into sending sexual images that he then shared on the dark web.

Courtesy BBC News


Advertisements help running this site for free.

To view the content please disable AdBlocker and refresh the page.