i heared that i should use compare() method for comparing two objects. instead of using - (substraction) directly. because -(substraction) can invoke overflow
public class Employee implements Comparable<Employee> {
...
public int compareTo(Employee other) {
return Double.compare(salary, other.salary); // don't use (-)
}
}
and when i saw this implemented CompareTo() code(in Interger.class)
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
and i could see '>'
but as i know it eventually would be using substraction like in c++... probably in interpreter.. if i'm right
and use this result 0 ,1,-1 as = < >
what's different?
why when i use compareTo() method i can avoid overflowing?
please give me a detail reason.