Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The < (less than) operator is a binary relational operator that compares the values of two numeric operands. It evaluates to the boolean value true if the left-hand operand is strictly lesser in mathematical value than the right-hand operand, and false otherwise.
operand1 < operand2

Operand Compatibility

The < operator strictly requires operands that can be resolved to numeric types.
  • Supported Primitives: byte, short, char, int, long, float, and double.
  • Unsupported Types: boolean, String, and all object references (with the exception of numeric wrapper classes).

Binary Numeric Promotion

Before performing the comparison, Java applies binary numeric promotion to the operands according to the Java Language Specification. This promotion occurs for all numeric comparisons based on the following strict hierarchy:
  1. If either operand is of type double, the other is promoted to double.
  2. Otherwise, if either operand is of type float, the other is promoted to float.
  3. Otherwise, if either operand is of type long, the other is promoted to long.
  4. Otherwise, both operands are promoted to int.
This means that operands of types smaller than int (byte, short, and char) are always widened to int prior to comparison, even if both operands are of the exact same type or if the broadest type between the two is smaller than int.
byte a = 10;
short b = 20;
boolean result1 = a < b; // Both 'a' and 'b' are promoted to int before comparison

int c = 5;
double d = 5.5;
boolean result2 = c < d; // 'c' is promoted to double (5.0) before comparison

Auto-Unboxing

If either operand is a numeric wrapper class (e.g., Integer, Double, Character), Java automatically unboxes the object into its corresponding primitive type before applying binary numeric promotion and evaluating the relational expression.
Integer x = Integer.valueOf(10);
int y = 20;
boolean result = x < y; // 'x' is unboxed to int 10 before comparison
Note: If the wrapper reference is null, the unboxing attempt will throw a NullPointerException.

Floating-Point Specifics

When evaluating float or double operands, the < operator adheres to IEEE 754 standards for floating-point arithmetic:
  • NaN (Not a Number): If either operand is NaN, the < operator always evaluates to false.
  • Infinity: Negative infinity (-Infinity) is considered strictly less than any finite value and positive infinity (+Infinity).
  • Signed Zeros: Positive zero (0.0) and negative zero (-0.0) are considered equal. Therefore, -0.0 < 0.0 evaluates to false.
boolean nanCheck = Double.NaN < 5.0;             // Evaluates to false
boolean infCheck = Double.NEGATIVE_INFINITY < 0; // Evaluates to true
boolean zeroCheck = -0.0 < 0.0;                  // Evaluates to false

Character Comparison

When applied to char operands, the < operator compares their 16-bit Unicode (UTF-16) integer values.
char c1 = 'A'; // Unicode value 65
char c2 = 'B'; // Unicode value 66
boolean result = c1 < c2; // Evaluates to true because 65 < 66
Master Java with Deep Grasping Methodology!Learn More