~/ 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/e2581eb0-6bac-4452-bc1c-018638c166da/flashcard utf-8 LF