Double.valueOf
implements the caching... Or does it???".The documentation is an obvious place to look for information, of course:
No obvious "frequently requested values" come to mind, aside from perhaps thepublic static Double valueOf(double d)
Returns aDouble
instance representing the specifieddouble
value. If anew Double
instance is not required, this method should generally be used in preference to the constructorDouble(double)
, as this method is likely to yield significantly better space and time performance by caching frequently requested values.
NaN
, the signed zeroes, the signed ones, and the signed infinities. Perhaps MIN/MAX_VALUE
and MIN_NORMAL
are also worth caching, since they at least do have the privilege of being defined as class constants.Either way, it's not as simple as having an array the way e.g.
Integer
instances are cached by Integer.valueOf
for values around zero.In my pursuit to uncover what is actually cached and how, I went straight to the source code.
public static Double valueOf(double d) { return new Double(d); }Ladies and gentlemen, we have been duped.