If the developer implemented the Comparable interface correctly, the method you proposed would be equivalent to java.util.Objects.equals(Object, Object).
Additionally both variants would require boxing for primitive types which I initially wanted to prevent. > Zheka Kozlov <orionllm...@gmail.com> hat am 6. Januar 2019 um 11:56 > geschrieben: > > > Why don't we just add a generic method which compares any instances of > Comparable? > > > public interface Comparator { > ... > > public static <T extends Comparable<? super T>> boolean equal(T x, T y) { > return x.compareTo(y) == 0; > } > } > > > Usage: > > Comparator.equal(1, 1); // true > Comparator.equal(1.0, 1.0); // true > Comparator.equal(2.0f, 2.0f); // true > Comparator.equal("abc", "def"); // false > Comparator.equal("abc", 1); // compilation error