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!

References



Share this page on WhatsApp

Get Free Tutorials by Email

About the Author

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