Search code examples
javaobjectinterfacecomparable

what does "other" mean in Java?


I have this code, but I fail to understand what "other" actually is, and what it's trying to do.

public interface Comparable<T>
{
    int compareTo(T other);
}

What does the parameter "other" suppose to mean?


Solution

  • It's just a parameter name, not a keyword. It's the other value you're comparing "this" value to. So suppose you're comparing two people, you might have:

    Person fred = new Person(...);
    Person george = new Person(...);
    int result = fred.compareTo(george);
    

    It would be up to the compareTo method to compare fred and george (which it would know as this and other) by whatever means it deemed appropriate, e.g. age, name etc.