TheDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
< (less than) operator in Kotlin is a relational operator used to evaluate the strict lower bound of one operand relative to another. Rather than being a strictly primitive language construct, Kotlin implements < via operator overloading and compile-time desugaring.
When the Kotlin compiler encounters the < operator, it translates the expression into a method call to the compareTo function. Specifically, the expression a < b is evaluated as a.compareTo(b) < 0.
The compareTo Contract
To support the < operator, the left-hand operand’s type must define a compareTo function that accepts the right-hand operand’s type and returns an Int. This is typically achieved by having the class implement the Comparable<T> interface.
The returned Int dictates the relational state:
- A negative integer (
< 0) indicates the left operand is less than the right operand. - Zero (
0) indicates equality in terms of ordering. - A positive integer (
> 0) indicates the left operand is greater than the right operand.
Custom Implementation Syntax
To enable the< operator for custom types, the type must provide a compareTo function. The function must be marked with the operator keyword for the compiler to bind it to the < symbol. However, if the class implements the Comparable<T> interface, the operator modifier is inherited automatically and should not be explicitly declared.
Type Constraints and Nullability
- Type Safety: The
<operator is statically typed. The compiler enforces that the right-hand operand matches the parameter type defined in the left-hand operand’scompareTosignature. - Nullability: The standard
<operator cannot be invoked directly on nullable types (T?). Operands must be non-null, or explicitly unwrapped, because the desugaredcompareTocall requires a non-null receiver. - Primitive Optimization: For primitive types like
Int,Float, andDouble, the<operator does not incur the overhead of a method call. The compiler optimizes the expression directly into the corresponding JVM bytecode comparison instructions (e.g.,if_icmplt,fcmpg), adhering to IEEE 754 standards for floating-point numbers.
Master Kotlin with Deep Grasping Methodology!Learn More





