CSE 116 Lecture Notes - Lecture 27: Negative Number
Document Summary
Provides simple, abstract way to compare data found in java. lang package just like string: makes coding easier by simplifying search & compare. Single method defined by comparable int compareto(e o: compare this instance with o(ther) instance return value unimportant, except for its relative value, sign of result shows ordering of this & o. Only if for all elements a & b either: a < b or a == b or a > b. Clicker question: null legal o in compareto()? public int compareto(nhlteam o) { //code comparing number of wins for this & o class nhlteam implements comparable : yes. null used like any element, no. Should trigger exception: yes. null is smallest element, yes. null is largest element, sort of. Make class comparable only when possible to compare & order with any instance allowed. //need to return zero in this case return 0; //need to return positive number here return 73;