Inside of Java’s ByteBuffer is the method compareTo for implementing Comparable<ByteBuffer>…
public int compareTo(ByteBuffer that) {
int n = this.position() + Math.min(this.remaining(), that.remaining());
for (int i = this.position(), j = that.position(); i < n; i++, j++) {
byte v1 = this.get(i);
byte v2 = that.get(j);
if (v1 == v2)
continue;
if ((v1 != v1) && (v2 != v2)) // For float and double
continue;
if (v1 < v2)
return -1;
return +1;
}
return this.remaining() - that.remaining();
}
What is the point of if ((v1 != v1) && (v2 != v2)) // For float and double?
If I write this kind of code, I’m warned about comparing identical expressions.
Double.NaN == Double.NaNandFloat.NaN == Float.NaNgivesfalse.See also end of JLS 4.2.3: