简体   繁体   English

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

[英]What is the difference between Double.parseDouble(String) and Double.valueOf(String)?

I want to convert String to a Double data type. 我想将String转换为Double数据类型。 I do not know if I should use parseDouble or valueOf . 我不知道是否应该使用parseDoublevalueOf

What is the difference between these two methods? 这两种方法有什么区别?

parseDouble returns a primitive double containing the value of the string: parseDouble返回包含字符串值的原始double:

Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double. 返回一个初始化为由指定String表示的值的新double,由double类的valueOf方法执行。

valueOf returns a Double instance, if already cached, you'll get the same cached instance. valueOf返回一个Double实例,如果已经缓存,您将获得相同的缓存实例。

Returns a Double instance representing the specified double value. 返回表示指定double值的Double实例。 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实例,通常应优先使用此方法,而不是构造函数Double(double),因为此方法可能通过缓存频繁请求的值来显着提高空间和时间性能。

To avoid the overhead of creating a new Double object instance, you should normally use valueOf 为避免创建新Double对象实例的开销,通常应使用valueOf

Double.parseDouble(String) will return a primitive double type. Double.parseDouble(String)将返回一个原始的double类型。 Double.valueOf(String) will return a wrapper object of type Double . Double.valueOf(String)将返回Double类型的包装器对象。

So, for eg: 所以,对于例如:

double d = Double.parseDouble("1");

Double d = Double.valueOf("1");

Moreover, valueOf(...) is an overloaded method. 而且, valueOf(...)是一个重载方法。 It has two variants: 它有两个变种:

  1. Double valueOf(String s)
  2. Double valueOf(double d)

Whereas parseDouble is a single method with the following signature: parseDouble是具有以下签名的单个方法:

  1. double parseDouble(String s)

They both convert a String to a double value but wherease the parseDouble() method returns the primitive double value, the valueOf() method further converts the primitive double to a Double wrapper class object which contains the primitive double value. 它们都将String转换为double值,但是当parseDouble()方法返回原始double值时,valueOf()方法进一步将原始double转换为包含原始double值的Double包装类对象。

The conversion from String to primitive double may throw NFE(NumberFormatException) if the value in String is not convertible into a primitive double. 如果String中的值不可转换为基本double,则从String到primitive double的转换可能会抛出NFE(NumberFormatException)。

parseDouble() method is used to initialise a STRING (which should contains some numerical value)....the value it returns is of primitive data type, like int, float, etc. parseDouble()方法用于初始化STRING(应该包含一些数值)....它返回的值是原始数据类型,如int,float等。

But valueOf() creates an object of Wrapper class. 但是valueOf()创建了一个Wrapper类的对象。 You have to unwrap it in order to get the double value. 您必须打开它才能获得double值。 It can be compared with a chocolate. 它可以与巧克力进行比较。 The manufacturer wraps the chocolate with some foil or paper to prevent from pollution. 制造商用一些箔或纸包裹巧克力以防止污染。 The user takes the chocolate, removes and throws the wrapper and eats it. 用户拿出巧克力,移除并扔掉包装并吃掉它。

Observe the following conversion. 请注意以下转换。

int k = 100; Integer it1 = new Integer(k);

The int data type k is converted into an object, it1 using Integer class. int数据类型k使用Integer类转换为对象it1。 The it1 object can be used in Java programming wherever k is required an object. it1对象可以在Java编程中用于需要k的对象。

The following code can be used to unwrap (getting back int from Integer object) the object it1. 以下代码可用于解包(从Integer对象返回int)对象it1。

int m = it1.intValue();

System.out.println(m*m); 的System.out.println(M * M); // prints 10000 //打印10000

//intValue() is a method of Integer class that returns an int data type. // intValue()是一个Integer类的方法,它返回一个int数据类型。

parseDouble()文档说“返回一个初始化为由指定String表示的值的新double,由double类的valueOf方法执行。”因此它们应该是相同的。

If you want to convert string to double data type then most choose parseDouble() method. 如果要将字符串转换为double数据类型,则大多数选择parseDouble()方法。 See the example code: 请参阅示例代码:

String str = "123.67";
double d = parseDouble(str);

You will get the value in double. 您将获得双倍的值。 See the StringToDouble tutorial at tutorialData . 请参阅tutorialData上的StringToDouble 教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM