2.6.7 - Numerical Comparison Operators

Numerical comparison operators are < (less than), > (greater than), <=, (less than or equal), >= (greater than or equal).

These operators are binary operators, their operands are primitive types or convertible to primitive types fails that, compile time error is generated. The action of these operators similar to each other and we will present them all together.

Comparison of integer and real data give absolutely reliable results. With integer data, signed integer comparison is performed. The reliability of signed comparison is out of any doubt. For real data, signed real comparison is conducted according to the IEEE 754 standard which states,

- If neither operands are NaN (Not a Number) then the result is false.
- The values are ordered from negative to positive infinity except NaN.
- Positive and negative zeroes are considered equal. This implies,
- -0.0 < 0.0 return false. Because, negative and positive zeroes are considered equal, assertion of inequality return false.
- -0.0 <= 0.0 return true. Because, negative and positive zeroes are considered equal, assertion of equality return true.

- For real data other than NaN, the values returned from the operator < is true for all values of the left operand less than the value of the right operand. Otherwise it will return false.

Application of numerical comparison operators is quite straightforward. One example is given below:

package preliminary; public class NumericalInequality { public static void main(String[] args){ int intNum = 389; double dblNum = 1000.12; boolean result = intNum < dblNum; System.out.print(" Result = " + result)); } // end main } //end class NumericalInequality

The result of this program :

Result = true

Before the comparison, the data type of the left ope4rand is automatically promoted to double and the comparion is realized , the result is true because 389.0 is less than 1000.12.