Java Reference
In-Depth Information
Less Than Operator (<)
The less than operator (
<
) is used in the form
operand1 < operand2
The less than operator returns
true
if
operand1
is less than
operand2
. Otherwise, it returns
false
. The operator
can be used only with primitive numeric data types. If either operand is
NaN
(
float
or
double
), the less than operator
returns
false
.
int i = 10;
int j = 15;
double d1 = Double.NaN;
boolean b;
b = (i < j); // Assigns true to b
b = (j < i); // Assigns false to b
// A compile-time error. < cannot be used with boolean operands
b = (true < false);
b = (d1 < Double.NaN); // Assigns false to b
Less Than or Equal to Operator (<=)
The less than or equal to operator (
<=
) is used in the form
operand1 <= operand2
The less than or equal to operator returns
true
if the value of
operand1
is less than or equal to the value of
operand2
. Otherwise, it returns
false
. The operator can be used only with primitive numeric data types. If either of
the operand is
NaN
(
float
or
double
), the less than or equal to operator returns
false
.
int i = 10;
int j = 10;
int k = 15;
boolean b;
b = (i <= j); // Assigns true to b
b = (j <= i); // Assigns true to b
b = (j <= k); // Assigns true to b
b = (k <= j); // Assigns false to b
Boolean Logical Operators
Table
4-4
lists Boolean logical operators available in Java. All Boolean logical operators can be used only with
boolean
operand(s). Subsequent sections will explain the usage of these operators in detail.