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

I want to convert String to a Double data type. I don't know if parseDouble or valueOf methods should be used?

What is the difference between these methods?

+47
java type-conversion
May 14 '12 at 4:46
source share
6 answers

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

Returns a new double initialized value represented by the specified string, which is executed by the valueOf method of the Double class.

valueOf returns an instance of Double, if it is already cached, you will 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 usually be used as an alternative to the Double (double) constructor, since this method is likely to provide significantly better space and time performance by caching frequently requested values.

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

+60
May 14 '12 at 4:49
source share
— -

Double.parseDouble(String s) will return a primitive double type. Double.valueOf(String s) will return a Wrapper Object type Double .

So for example

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

Morover, valueOf( ) is an overloaded method. It has two options:

  • Double valueOf(String s)
  • Double valueOf(Double d)

    Where as parseDouble there is a single method with the following signature:

  • double parseDouble(String s)

+27
May 14 '12 at 5:28
source share

Both of them convert String to a double value, but wherease the parseDouble () method returns a primitive double value, the valueOf () method further converts the primitive double to an object of the Double wrapper class that contains a primitive double value.

Converting from String to a primitive double can raise NFE (NumberFormatException) if the value in String is not converted to a primitive double.

+1
May 14 '12 at 6:30
source share

The parseDouble () method is used to initialize STRING (which should contain some numeric value) .... the return value is of primitive data type, for example, int, float, etc.

But valueOf () creates an object of class Wrapper. You must expand it to get a double value. It can be compared to chocolate. The manufacturer wraps the chocolate with foil or paper to prevent contamination. The user accepts the chocolate, removes and throws the wrapper and eats it.

Pay attention to the following conversion.

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

The int k data type is converted to an object, it1, using the Integer class. The it1 object can be used in Java programming, where k requires an object.

The following code can be used to expand (return an int from an Integer object) of the it1 object.

 int m = it1.intValue(); 

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

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

+1
Jun 25 '14 at 6:20
source share

The documentation for parseDouble() says: "Returns the new double initialized value represented by the specified string, as performed by the valueOf method of the Double class.", So they must be identical.

0
May 14 '12 at 4:49
source share

If you want to convert a string to a double data type, then select the parseDouble () method. See Code Example:

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

You will get the value in double size. See StringToDouble Tutorial in tutorialData .

0
May 14 '12 at 5:09
source share



All Articles