Java examples for Language Basics:Operator
All relational operators are binary operators.
List of Relational Operators in Java
Operators | Meaning | Type | Usage | Result |
---|---|---|---|---|
== | Equal to | Binary | 3 == 2 | false |
!= | Not equal to | Binary | 3 != 2 | true |
> | Greater than | Binary | 3 > 2 | true |
>= | Greater than or equal to | Binary | 3 >= 2 | true |
< | Less than | Binary | 3 < 2 | false |
<= | Less than or equal to | Binary | 3 <= 2 | false |
double d1 = 0.0; double d2 = -0.0; boolean b = (d1 == d2); // Assigns true to b
A positive infinity is equal to another positive infinity.
A negative infinity is equal to another negative infinity. However, a positive infinity is not equal to a negative infinity.
double d1 = Double.POSITIVE_INFINITY; double d2 = Double.NEGATIVE_INFINITY; boolean b1 = (d1 == d2); // Assigns false to b1 boolean b2 = (d1 == d1); // Assigns true to b2
If either operand is NaN, the equality test returns false.
double d1 = Double.NaN; double d2 = 5.5; boolean b = (d1 == d2); // Assigns false to b
Note that even if both the operands are NaN, the equality operator will return false.
d1 = Double.NaN;
d2 = Double.NaN;
b = (d1 == d2); // Assigns false to b
Float and Double classes have an isNaN() method, which accepts a float and a double argument, respectively.
It returns true if the argument is NaN, Otherwise, it returns false.
double d1 = Double.NaN; b = Double.isNaN(d1); // Assigns true to b. Correct way to test for a NaN value