Double.valueOf (s) vs Double.parseDouble

Moving the object to double and noticed both of these methods. I see that parseDouble is from 1.2. Why add this method if it essentially performs the same functions as valueOf (s)?

+22
java methods
Aug 31 '11 at 9:31
source share
3 answers

parseDouble() returns the primitive double value. valueOf() returns an instance of the double wrapper class. Before Java 5 introduced autoboxing, that was a very significant difference (and many claim it still exists).

+30
Aug 31 2018-11-11T00
source share

Because it is not the same thing. valueOf() creates a Double object, which is often not needed. parseDouble() no. With autoboxing, it has valueOf(String) , which is no longer needed, but therefore has backward compatibility.

+10
Aug 31 2018-11-11T00:
source share

If you just need to use a value (primitive), use parseDouble(String s) , the cost is less. valueOf(String s) returns a double class that wraps a primitive double value.

+4
Aug 31 '11 at 9:43
source share



All Articles