在我的工作中,所有开发人员都使用Double.valueOf而不是new Double构造函数。在每种情况下。对于 Integer 或 Short 我可以理解它的缓存值但不是 fordouble和float。
我在 OpenJDK 源代码中查看 Double.valueOf :
/**
* Returns a {@code Double} instance representing the specified
* {@code double} value.
* If a new {@code Double} instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Double(double)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested …Run Code Online (Sandbox Code Playgroud)