Java Comparable Comparison Comparison Method

I do not see anything that I am doing wrong, but NetBeans gives me the following error:

incomparable types
required: boolean
found: java.lang.Object


public int compareTo(Object obj)  {
    if( obj instaceof Employee){
       Employee employee = (Employee) obj;
       if(this.weekly_earnings > employee.weekly_earnings)
           return 1;
       else if(this.weekly_earnings == employee.weekly_earnings)
           return 0;
       else
           return -1;
    }
    else{
        System.out.println("Error");
    }
}
+3
source share
1 answer

It is written in it instanceof.

Also, as Tom Hotin mentioned in a comment, if you are using Java 1.5 or later, you can write compareTo(Employee emp)to not use it at all instanceof. There was a detailed section on writing comparable types in the Object Ordering Java Tutorial.

+6
source

Source: https://habr.com/ru/post/1735521/


All Articles