diff options
Diffstat (limited to 'java/lang/Double.java')
-rw-r--r-- | java/lang/Double.java | 38 |
1 files changed, 24 insertions, 14 deletions
diff --git a/java/lang/Double.java b/java/lang/Double.java index a05514368..f3f7cb1e0 100644 --- a/java/lang/Double.java +++ b/java/lang/Double.java @@ -104,6 +104,16 @@ public final class Double extends Number implements Comparable<Double> public static final Class<Double> TYPE = (Class<Double>) VMClassLoader.getPrimitiveClass('D'); /** + * Cache representation of 0 + */ + private static final Double ZERO = new Double(0.0d); + + /** + * Cache representation of 1 + */ + private static final Double ONE = new Double(1.0d); + + /** * The immutable value of this Double. * * @serial the wrapped double @@ -261,8 +271,12 @@ public final class Double extends Number implements Comparable<Double> */ public static Double valueOf(double val) { - // We don't actually cache, but we could. - return new Double(val); + if ((val == 0.0) && (doubleToRawLongBits(val) == 0L)) + return ZERO; + else if (val == 1.0) + return ONE; + else + return new Double(val); } /** @@ -277,7 +291,7 @@ public final class Double extends Number implements Comparable<Double> */ public static Double valueOf(String s) { - return new Double(parseDouble(s)); + return valueOf(parseDouble(s)); } /** @@ -490,17 +504,13 @@ public final class Double extends Number implements Comparable<Double> */ public boolean equals(Object obj) { - if (! (obj instanceof Double)) - return false; - - double d = ((Double) obj).value; - - // Avoid call to native method. However, some implementations, like gcj, - // are better off using floatToIntBits(value) == floatToIntBits(f). - // Check common case first, then check NaN and 0. - if (value == d) - return (value != 0) || (1 / value == 1 / d); - return isNaN(value) && isNaN(d); + if (obj instanceof Double) + { + double d = ((Double) obj).value; + return (doubleToRawLongBits(value) == doubleToRawLongBits(d)) || + (isNaN(value) && isNaN(d)); + } + return false; } /** |