Return to Unit Menu | Java Main Page | | Terms of Use


Relational Operators
There are six relational (comparison) operators used for data comparisons in Java. 

Operator Description
= = equal to
! =  not equal to
> greater than
> =  greater than or equal to
< less than
< =  less than or equal to

Check out this numerical example:  

Assume these values are assigned:

int a = 4;
int b = 12;
int c = 16;
int d = 4;

... then the following are true:

a = = d
b < c
c > a


Notice that ONE equal sign is used to "assign" a value, but TWO equal signs are used to check to see if numerical values are equal to one another.

Relational operators always yield a true or false result. 
if ( (x > 5) = = true)
(does not accept 0 as false and 1 as true as you may expect)

Relational operators have a lower precedence than the arithmetic operators.  This means that an expression such as  x + 4 > = y - 5
is the same as (x + 4) > = (y - 5).

Caution:  The = = is NOT used with String variables.
Remember, String variables are stored in memory by storing addresses to the actual string text.  If we tried to use the = = operator, we would be comparing the addresses and not the actual string text.

 To test two String variables (or any two objects) to see if they have equal values, you must use the method equals rather than = =.
s1.equals(s2)       //checks for equality
//checks for equality ignoring case



Return to Unit Menu | Java Main Page | | Terms of Use