Feature suggestion: Add static equals methods to Float and Double

Remi Forax forax at univ-mlv.fr
Fri Jan 4 23:50:13 UTC 2019

it's not obvious to me that this is a source backward compatible change.

if you have something like:
  interface Fun { boolean eq(Double a, double b); }
  Fun fun = Double::equals; 

it's not clear to me which variant will be selected ?


----- Mail original -----
> De: some-java-user-99206970363698485155 at vodafonemail.de
> À: "core-libs-dev" <core-libs-dev at openjdk.java.net>
> Envoyé: Samedi 5 Janvier 2019 00:11:09
> Objet: Feature suggestion: Add static equals methods to Float and Double

> To test whether primitive float or double values are equal according to
> `Float.equals` and `Double.equals` you either have to create wrapper instances
> for them (possible performance decrease), use the respective static `compareTo`
> (verbose) or have to use the appropriate methods (`floatToIntBits` /
> `doubleToLongBits`) (verbose and error-prone since you could confuse them with
> the other conversion methods).
> It would be good to provide static methods for testing for equality of the
> primitive values:
> // In Float.java
> public static boolean equals(float a, float b) {
> return Float.floatToIntBits(a) == Float.floatToIntBits(b);
> }
> // In Double.java
> public static boolean equals(double a, double b) {
> return Double.doubleToLongBits(a) == Double.doubleToLongBits(b);
> }
> This would be very convenient for developers and prevent them from writing
> (possibly faulty) code for this themselves.

More information about the core-libs-dev mailing list