-3

Can anyone explain how does the following codes works?

public int compareTo(Object o) {
    if (o instanceof Item) {
        return date1.compareTo( ((Item) o).getDate2());
    }
    return -1;
}
bluish
  • 26,356
  • 27
  • 122
  • 180
user1503699
  • 55
  • 1
  • 3
  • 8
  • 1
    Its impossible to explain the code without knowing what `getDate2()` returns. – adamjmarkham Jul 06 '12 at 02:07
  • 1
    The code does exactly what it does. There's no way to explain *why* the code does that without seeing a broader context. By its self, that method looks a little odd. – Corbin Jul 06 '12 at 02:07
  • Can not explain without more code . At least , the definition of date1 and getDate2() . – Sabbath Jul 06 '12 at 02:10
  • Do not use Stack Overflow when you could read the appropriate documentation. We do not explain *how code works*, we help you *solve programming problems*. – Anthony Pegram Jul 06 '12 at 02:15

1 Answers1

0

It check that object o is an item, then compares them, the compare to function returns the values, -1, 0 and 1.

If it returns a value of 0 it means the objects are the same, if it return -1 or 1 it is saying that they are not the same.

The return at the end is the default catch, so if object o is not of that item type return no match.

Sean F
  • 2,352
  • 3
  • 28
  • 40