Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double.valueOf(s) vs. Double.parseDouble [duplicate]

Tags:

java

methods

Casting an Object to a double and noticed both these methods. I see that parseDouble has been in since 1.2. Why add this method if it essentially does the same functionality as valueOf(s)?

like image 656
Will Avatar asked Aug 31 '11 09:08

Will


People also ask

What is the difference between double valueOf and double parseDouble?

valueOf() creates a Double object which is often not needed. parseDouble() does not. With autoboxing it's valueOf(String) which is no longer needed, but is therefore backward compatibility.

What is double parseDouble?

Double parseDouble() method in Java with examples The parseDouble() method of Java Double class is a built in method in Java that returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double. Syntax: public static double parseDouble(String s)

What is double valueOf?

valueOf(String s) method returns a Double object holding the double value represented by the argument string s. If s is null, then a NullPointerException is thrown.

What is double valueOf in Java?

ValueOf(Double) Returns a Double instance representing the specified double value. ValueOf(String) Returns a Double object holding the double value represented by the argument string s .


2 Answers

parseDouble() returns a primitive double value. valueOf() returns an instance of the wrapper class Double. Before Java 5 introduced autoboxing, that was a very significant difference (and many would argue it still is).

like image 68
Michael Borgwardt Avatar answered Sep 19 '22 21:09

Michael Borgwardt


Because it is not the same. valueOf() creates a Double object which is often not needed. parseDouble() does not. With autoboxing it's valueOf(String) which is no longer needed, but is therefore backward compatibility.

like image 37
Peter Lawrey Avatar answered Sep 21 '22 21:09

Peter Lawrey