~/ learn/ java-from-zero/ cards/ Comparable<T> vs Comparator<T>
1 of 4

Type a compareTo using Integer.compare

Type a compareTo using Integer.compare

Answer

@Override public int compareTo(Student other) { return Integer.compare(this.gpa, other.gpa); }

Integer.compare avoids integer overflow bugs that bite this.gpa - other.gpa. Returns negative/zero/positive as required by the Comparable contract.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/bf4a596f-f0da-4a21-bcba-f2e1f61f1721/flashcard utf-8 LF