-3

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).

Jacob G.
  • 26,421
  • 5
  • 47
  • 96
Yohan Malshika
  • 415
  • 6
  • 17
  • 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 Answers2

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
  • 20,803
  • 4
  • 23
  • 45
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
  • 8,605
  • 144
  • 77
  • 113
Mayur Giri
  • 21
  • 1