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)?
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.
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)
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.
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 .
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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With