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.3k times
3 Answers
53
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
- 335,521
- 76
- 467
- 706
-
Nice I just discovered the difference via some overloading. Now to look up autoboxing – Will Aug 31 '11 at 09:39
19
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
- 85,564
- 27
- 164
- 223
Peter Lawrey
- 513,304
- 74
- 731
- 1,106
6
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,092
- 1
- 8
- 22