5
public int compareTo(Object x) {
    Task other = (Task)x;

    if (other.priority == this.priority) {
        System.out.println("Each task has the same priority level.");
        return 0;
    } else if (other.priority > this.priority) {
        System.out.println(other.priority + "\n" + this.priority);
        return -1;
    } else {
        System.out.println(this.priority + "\n" + other.priority);
        return 1;
    }
}

That's the code I have for a programming assignment for class. I'm not sure why I use Task other = (Task)x; or what it's doing here. The rest I understand. If anyone has a quick explanation of what that's actually doing here I would be greatful. Thank you!

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Sarah Diri
  • 97
  • 1
  • 9
  • 1
    Are you implementing `Comparable` or some other interface? If so, it would be worth adding the class declaration. – Didier L Dec 10 '15 at 12:41

2 Answers2

2

You are casting Object x to Task other - Object and Task are different types, so you need the cast so you can treat x as the expected type (and get to it's fields).

Normally in compareTo() you would first have something like if (x instanceof Task) before blindly casting it - if you don't and the types are different then things will crash)

John3136
  • 28,809
  • 4
  • 51
  • 69
2

The method signature takes an Object type object so in order to reference the variable priority within the object passed in, it has to do a cast to the Task object as the variable only exists within the Task class.

Personally though, I would think this was bad practice as you do not know what sort of object is being passed in (any class subclassing Object can be passed in) so a instance of check would not go amiss less you want to run into a runtime error of a ClassCastException.

Alternatively, you could use generics to specify what sort of object you want to compare to. So rather than doing this...

public class Task implements Comparable {
    private int priority = 1;

    @Override
    public int compareTo(Object o) {
        if (o instanceof Task) {
            Task t = (Task) o;
            return this.priority < t.priority;
        }

        return -1;
    }
}

...you could do this...

public class Task implements Comparable<Task> {
    private int priority = 1;

    @Override
    public int compareTo(Task t) {
        return this.priority < t.priority;
    }
}
Ocracoke
  • 1,718
  • 3
  • 24
  • 38
  • Would I be able to change `Object x` to something more specific (in this case, `Task x` maybe)? I was under the impression from the lesson itself that that was just 'how it was done'. – Sarah Diri Dec 10 '15 at 02:18
  • @SarahDiri The only two ways to "change" `x` from `Object` to `Task` is to either do the cast (which you are already doing) or change the method parameter to only allow `Task` objects to be passed in as the parameter. Java allows for this by using generics (so `implements Comparable` instead of `implements Comparable`). Using this would allow for only `Task` objects to be passed in. – Ocracoke Dec 10 '15 at 11:29
  • See also [What is a raw type and why shouldn't we use it?](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it) – Didier L Dec 10 '15 at 12:39