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) operator is a relational operator used to evaluate strict upper-bound inequality between two operands. In Kotlin, it functions as syntactic sugar that the compiler translates into a method call to the compareTo function. When the compiler encounters a > b, it desugars the expression to evaluate whether the integer returned by compareTo is strictly greater than zero.
val a = 10
val b = 5

// Standard operator syntax
val isGreater = a > b

// Compiler-translated equivalent
val isGreaterTranslated = a.compareTo(b) > 0

Type Requirements

For the > operator to be valid on a given type, the left-hand operand must provide an operator fun compareTo that accepts the right-hand operand’s type and returns an Int. This is structurally achieved in two ways:
  1. Implementing the Comparable<T> interface (which inherently provides the operator modifier).
  2. Defining a standalone operator fun compareTo as a member or extension function.
// Method 1: Implementing Comparable<T>
class Measure(val magnitude: Int) : Comparable<Measure> {
    override fun compareTo(other: Measure): Int {
        return this.magnitude.compareTo(other.magnitude)
    }
}

// Method 2: Standalone operator function
class Weight(val kg: Double) {
    operator fun compareTo(other: Weight): Int {
        return this.kg.compareTo(other.kg)
    }
}

Return Value Contract

The underlying compareTo function must adhere to a strict mathematical contract, returning:
  • A positive integer (> 0) if the receiver object is strictly greater than the argument.
  • Zero (0) if the objects are equal.
  • A negative integer (< 0) if the receiver object is less than the argument.

Floating-Point Behavior

When the > operator is used with statically typed Float or Double primitives, the compiler generates JVM bytecodes (fcmpg, dcmpg) that strictly adhere to IEEE 754 floating-point comparison rules. Under these rules, any comparison involving NaN evaluates to false, and -0.0 is evaluated as exactly equal to 0.0. However, if the operands are boxed or evaluated generically via the Comparable interface, Kotlin delegates to the Float.compareTo and Double.compareTo methods. These methods deviate from IEEE 754 to establish a total order:
  • NaN is considered equal to itself and strictly greater than POSITIVE_INFINITY.
  • 0.0 is considered strictly greater than -0.0.
// IEEE 754 primitive comparison
val primitiveNanGreater = Float.NaN > Float.POSITIVE_INFINITY // Evaluates to false
val primitiveZeroGreater = 0.0 > -0.0                         // Evaluates to false

// Generic Comparable comparison
val genericNan: Comparable<Float> = Float.NaN
val genericZero: Comparable<Double> = 0.0

val genericNanGreater = genericNan > Float.POSITIVE_INFINITY  // Evaluates to true
val genericZeroGreater = genericZero > -0.0                   // Evaluates to true
Master Kotlin with Deep Grasping Methodology!Learn More