Java Reference
In-Depth Information
Relational Operators
All relational operators are binary operators. That is, they take two operands. The result produced by a relational
operator is always a Boolean value true or false. Table
4-3
lists the relational operators available in Java.
Table 4-3.
List of Relational Operators in Java
Operators
Meaning
Type
Usage
Result
==
3 == 2
false
Equal to
Binary
!=
3 != 2
true
Not equal to
Binary
>
3 > 2
true
Greater than
Binary
>=
3 >= 2
true
Greater than or equal to
Binary
<
3 < 2
false
Less than
Binary
<=
3 <= 2
false
Less than or equal to
Binary
Equality Operator (==)
The equality operator (
==
) is used in the form
operand1 == operand2
The equality operator is used to test two operands for equality. It uses the following rules:
•
Both operands must be either primitive type or reference type. Mixed operands types are not
allowed. Mixing the operands types results in a compile-time error.
true
if the both operands represent the same value;
otherwise, it returns
false
. If both operands must be either numeric or
boolean
. A mix of
numeric and
boolean
types is not allowed.
•
For primitive operands, it returns
true
if the both operands refer to the same object in
memory; otherwise it returns
false
.
Suppose there is an
int
variable
i
.
•
For reference operands, it returns
int i = 10;
Now,
i == 10
will test whether
i
is equal to 10 or not. Because
i
is equal to
10
, the expression
i == 10
will
evaluate to
true
.
Let's consider another example.
int i;
int j;
int k;
boolean b;
i = j = k = 15; // Assign 15 to i, j, and k
b = (i == j == k); // A compile-time error