Operator Overloading in Kotlin

Comparison operator compares two values, we can overload the comparison operator either by overloading CompareTo() or equals method, both these two methods are part of Any Class.

Important : equals method and CompareTo functions are already overloaded in Any class, so we cannot overload them in our class. But if you want to provide new meaning for these methods, then you must override them (as overloading is prohibited)

Let's learn about overriding the equals method in kotlin, equals functions compares two objects and verifies whether both are same or not. Here, the equals method is invoked by "==" operator and you cannot override the "===" operator

Equals function should return Boolean value as a result, so here you will be able to judge what is higher(larger) or lesser(smaller)

import java.awt.Point
fun main(args:Array<String>)
{
    // both are diffrent
    println(EqualsOperator(10,20) == EqualsOperator(10,21))
    // both point at same address
    println(EqualsOperator(10,20) == EqualsOperator(10,20))
    // you cannot compare diffrent type,
    // kotlin compiler is intelligent enough to say
    // you cannot compare different types
    // but you are free to un-comment below and use
    /*println(EqualsOperator(10,20) == Point(10,20))*/
}
class EqualsOperator(val a:Int, val b:Int){
    override fun equals(obj:Any?):Boolean{
        println("comparing values by custom equals")
        // check for the type, if it is not same type then both are different
        if(obj !is EqualsOperator){
            return false
        }
        // compare whether both point to same address
        if(obj === this){
            return true
        }
        // if not same object(at address) at least compare values
        return a == obj.a && b == obj.b
    }
}

Actually, you cannot overload the logic of the CompareTo() function, but you can override the method just like equals.

CompareTo() function results in Int value like below

  • value1 > value2 : if value1 is greater than value2 then > operator returns true, calls a.compareTo(b) > 0
  • value1 < value2 : if value1 is less than value2 then < operator returns true, calls a.compareTo(b) < 0
  • value1 >= value2 : if value1 is greater than or equal to value2 then >= operator returns true, calls a.compareTo(b) >= 0
  • value1 <= value2 : if value1 is lesser than or equal to value2 then <= operator returns true, calls a.compareTo(b) <= 0
  • value1 == value2 : value1 is equal to value2
    When using compareTo you have to override the method in the comparable interface
  • To Indicate a is less than b, it returns a negative integer
  • When b is greater than a, it a returns positive integer
  • For equality, it returns 0

Let's override the CompareTo() function with help of compareValuesBy functions

fun main(args:Array<String>)
{
    // both are diffrent
    println(EqualsOperator(10,20).compareTo(EqualsOperator(10,21)))
    // both are same
    println(EqualsOperator(10,20).compareTo(EqualsOperator(10,20)))
    // both are diffrent
    println(EqualsOperator(10,21).compareTo(EqualsOperator(10,20)))
    // use relational operators to get whether true or false values
    // both are diffrent
    println(EqualsOperator(10,20) > EqualsOperator(10,21))
    // bot are same values
    println(EqualsOperator(10,20) < EqualsOperator(10,20))
    // both are diffrent
    println(EqualsOperator(10,21) >= EqualsOperator(10,20))
}
class EqualsOperator(val a:Int, val b:Int): Comparable<EqualsOperator>{
    override fun compareTo(obj:EqualsOperator):Int{
        return compareValuesBy(this, obj,
                EqualsOperator::a, EqualsOperator::b)
    }
}

Explanation:

: Comparable<EqualsOperator> We are extending the Comparable with type of EqualsOperator

compareValuesBy(this, obj, EqualsOperator::a, EqualsOperator::b) we are telling the functions what are to compare, like compare a, b from this object and obj Object

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions