java - Double.parseDouble(String) 和 Double.valueOf(String) 有什么区别?

标签 java type-conversion

我想将 String 转换为 Double 数据类型。我不知道我应该使用 parseDouble 还是 valueOf

这两种方法有什么区别?

最佳答案

parseDouble返回包含字符串值的原始 double :

Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.

valueOf返回一个 Double 实例,如果已经缓存,您将获得相同的缓存实例。

Returns a Double instance representing the specified double value. If a new Double instance is not required, this method should generally be used in preference to the constructor Double(double), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

为避免创建新的 Double 对象实例的开销,您通常应该使用 valueOf

关于java - Double.parseDouble(String) 和 Double.valueOf(String) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10577610/

相关文章:

php - __toString 魔术和类型强制

SASS:不兼容的单位: 'vw' 和 'px'

java - Pentaho : status 404 error on CentOS

powershell - 将输入变量转换为 Uint64

java - 将项目作为库链接到其他项目

java - 自动编译线程类中的类。请解释

c - 是否有一种故障安全方法来确定 C 中指针的对齐/尾随位?

在 C 中将字符串转换为 mpz_t

java - Java标准版有多少个类?

java - Java 中的队列允许删除随机元素。这不好吗?