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

JavaType Conversion

Java Problem Overview


I want to convert String to a Double data type. I do not know if I should use parseDouble or valueOf.

What is the difference between these two methods?

Java Solutions


Solution 1 - Java

parseDouble returns a primitive double containing the value of the string:

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

valueOf returns a Double instance, if already cached, you'll get the same cached instance.

> 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.

To avoid the overhead of creating a new Double object instance, you should normally use valueOf

Solution 2 - Java

Double.parseDouble(String) will return a primitive double type. Double.valueOf(String) will return a wrapper object of type Double.

So, for e.g.:

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

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

Moreover, valueOf(...) is an overloaded method. It has two variants:

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

Whereas parseDouble is a single method with the following signature:

  1. double parseDouble(String s)

Solution 3 - Java

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.

But valueOf() creates an object of Wrapper class. You have to unwrap it in order to get the double value. 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. The it1 object can be used in Java programming wherever k is required an object.

The following code can be used to unwrap (getting back int from Integer object) the object it1.

int m = it1.intValue();

System.out.println(m*m); // prints 10000

//intValue() is a method of Integer class that returns an int data type.

Solution 4 - Java

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.

The conversion from String to primitive double may throw NFE(NumberFormatException) if the value in String is not convertible into a primitive double.

Solution 5 - Java

Documentation for parseDouble() says "Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.", so they should be identical.

Solution 6 - Java

If you want to convert string to double data type then most choose parseDouble() method. 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBobsView Question on Stackoverflow
Solution 1 - JavaMByDView Answer on Stackoverflow
Solution 2 - JavaPriyank DoshiView Answer on Stackoverflow
Solution 3 - JavarohanproView Answer on Stackoverflow
Solution 4 - JavaSurender ThakranView Answer on Stackoverflow
Solution 5 - JavaRob IView Answer on Stackoverflow
Solution 6 - JavaRajshriView Answer on Stackoverflow