Double.valueOf(s) vs. Double.parseDouble [duplicate]
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)?
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 just need the value (primitive) use parseDouble(String s)
the cost is less. valueOf(String s)
returns a Double class which wraps the primitive double value.
精彩评论