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)?
Asked
Active
Viewed 4.8k times
3 Answers
55
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).
Michael Borgwardt
- 342,105
- 78
- 482
- 720
-
Nice I just discovered the difference via some overloading. Now to look up autoboxing – Will Aug 31 '11 at 09:39
20
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.
Buhake Sindi
- 87,898
- 29
- 167
- 228
Peter Lawrey
- 525,659
- 79
- 751
- 1,130
7
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.
Victor Martinez
- 1,102
- 2
- 8
- 22