Java Fundamentals  «Prev 

Java Relational Operators

Equality operator

The equality operator consists of two equals signs and when evaluated will return either a true or a false value. The assignment operator uses a single equal sign and will modify its left operand. To illustrate these operators, consider the following example. If a value of a rate variable equals 100, we could assume that an error is present. To reflect this error condition we could assign a true value to the errorPresent variable. This can be performed using both the assignment and the equality operators.

int rate;
rate = 100;
boolean errorPresent = rate==100;
System.out.println(errorPresent);

When the preceding code snippet is executed we get the following output:
true

The logical expression, rate==100, compares the value stored in rate to the integer literal 100. If they are equal, which is the case here, the expression returns true. The true value is then assigned to errorPresent. If the value stored in rate had not been 100, then the expression will return a value of false.

Relational Operators

Relational operators are used to determine the relationship, or relative ordering, between two operands. These operators frequently use two symbols. For example, greater than or equal to is expressed using the >= operator. The ordering of the symbols is important. Using => is not legal.

Java Relational Operators
The relational operators are listed in the table above: