Is there a potential for overflow if I were to write the following:
public class SomeObj implements Comparable<SomeObj> {
private final float data;
public SomeObj(float data) {
this.data = data;
}
public int compareTo(SomeObj object) {
return (int) (this.data - object.data);
}
}
I've seen other Java developers write their compareTo
methods like how I did above as a shortcut instead of writing a bunch of if-else
statements. Would the latter be the best approach to implementing the compareTo
method here if there really is a potential for overflow?