user69514
user69514

Reputation: 27629

Java Comparable Interface compareTo method

I don't 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");
    }
}

Upvotes: 3

Views: 7186

Answers (1)

Bill the Lizard
Bill the Lizard

Reputation: 405745

It's spelled instanceof.

Also, as Tom Hawtin mentioned in a comment, if you're using Java 1.5 or later you can write compareTo(Employee emp) to avoid using instanceof at all. There's a thorough section on writing Comparable types in the Object Ordering Java tutorial.

Upvotes: 8

Related Questions