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 >= (greater-than-or-equal-to) operator is a relational operator used to evaluate the structural or numerical order of two operands. In Kotlin, relational operators are not strictly primitive operations; rather, >= acts as syntactic sugar that delegates to the compareTo method, which is typically provided by implementing the Comparable<T> interface. When the Kotlin compiler encounters the >= operator, it translates the expression into a method call that evaluates the integer result of compareTo against zero.
// Operator syntax
val result = a >= b

// Desugared equivalent
val result = a.compareTo(b) >= 0

The compareTo Contract

For the >= operator to function on a given type, the left-hand operand (the receiver) must possess a compareTo function that accepts the right-hand operand’s type and returns an Int. The returned integer dictates the relational outcome:
  • Positive (> 0): The left operand is greater than the right.
  • Zero (== 0): Both operands are equal in order.
  • Negative (< 0): The left operand is less than the right.
The >= operator evaluates to true if the underlying compareTo call yields a positive integer or zero.

Operator Overloading

To enable the >= operator for custom classes, the class must either implement the Comparable<T> interface or define a custom compareTo function explicitly marked with the operator keyword.
class CustomValue(val magnitude: Int) : Comparable<CustomValue> {
    // The 'operator' modifier is implicitly applied via the Comparable interface
    override fun compareTo(other: CustomValue): Int {
        return this.magnitude.compareTo(other.magnitude)
    }
}

// Alternatively, as a standalone operator function without the interface:
class AnotherValue(val magnitude: Int) {
    operator fun compareTo(other: AnotherValue): Int {
        return this.magnitude.compareTo(other.magnitude)
    }
}

Type Resolution and Nullability

  • Type Safety: The Kotlin compiler enforces type safety during the desugaring process. The type of the right-hand operand must be compatible with the parameter type expected by the left-hand operand’s compareTo function.
  • Nullability: The >= operator cannot be used directly on nullable types (T?) because the standard library Comparable interface requires non-null receivers and parameters. Attempting to use >= on a nullable type results in a compilation error unless a custom extension function (e.g., operator fun T?.compareTo(other: T?): Int) is explicitly defined in the scope.
  • Floating-Point Behavior: When used with statically typed Float or Double primitives, the >= operator adheres strictly to IEEE 754 standards (e.g., NaN >= NaN evaluates to false). However, if the operands are boxed and compared generically via Comparable<Float> or Comparable<Double>, the comparison falls back to total order behavior, where NaN is considered equal to itself and greater than POSITIVE_INFINITY.
Master Kotlin with Deep Grasping Methodology!Learn More