I don't know how to convert a String
to a double
. How can I use Double.parseDouble(String)
and Double.valueOf(double)
? I would also like to know the difference between Double.parseDouble(String)
and Double.valueOf(double)
.
Asked
Active
Viewed 2,786 times
-2

Jacob G.
- 28,856
- 5
- 62
- 116

Yohan Malshika
- 716
- 1
- 11
- 20
-
you should use Double.parseDouble(String) if you want to change string to double but the difference between Double.parseDouble(String) and Double.valueOf(double) is that the first gets string as an argument and later gets double as an argument – parsa Jan 27 '18 at 01:59
-
@Parsa there are two versions of `Double.valueOf()` method. One takes a `double` as an argument, other takes a `String` as an argument. – Yousaf Jan 27 '18 at 02:01
-
in that case there are no differences between these two except the fact that parseDouble returns a new double whereas the other one doesnt – parsa Jan 27 '18 at 02:09
-
@Parsa There are more difference, don't ignore the return type. – Tom Jan 27 '18 at 02:57
2 Answers
4
how to convert string to double
String str = "0.222";
double dstr = Double.parseDouble(str);
I would also like to know the difference between Double.parseDouble(String) and Double.valueOf(double).
.parseDouble()
returns a primitive double
whereas .valueOf()
returns an instance of Double
class.
See official doc for Double class for more details.

Yousaf
- 27,861
- 6
- 44
- 69
2
The basic difference is that parseDouble()
returns a double
which is a primitive data type while valueOf()
returns an object of Double
which is a wrapper class.

Pang
- 9,564
- 146
- 81
- 122

Mayur Giri
- 21
- 1