开发者

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

This question already has answers here: 开发者_JS百科 What is the difference between Double.parseDouble(String) and Double.valueOf(String)? (6 answers) Closed 9 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜