Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the difference between Double.valueOf(String s) and Double.ParseDouble(String s)?

Tags:

java

As I understand the doc, ParseDouble function made something like :

 Double parseDouble(String s) throws ... {       
      return new Double(Double.valueOf(s));
 }
like image 986
oyo Avatar asked Sep 14 '10 12:09

oyo


People also ask

What does double parseDouble do?

Double parseDouble() method in Java with examples The parseDouble() method of Java Double class is a built in method in Java that returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double.

What is double valueOf?

valueOf(String s) method returns a Double object holding the double value represented by the argument string s. If s is null, then a NullPointerException is thrown.

What is the use of parseDouble () method?

parseDouble. Returns a new double initialized to the value represented by the specified String , as performed by the valueOf method of class Double .

How do you know if a String is Parsable to double?

Therefore, to know whether a particular string is parse-able to double or not, pass it to the parseDouble method and wrap this line with try-catch block. If an exception occurs this indicates that the given String is not pars able to double.


1 Answers

The logic is the same, but the return value of Double.valueOf() return a heap allocated Double object, where as parseDouble returns a primitive double. Your code example is not quite correct. The java source reads:

public static double parseDouble(String s) throws NumberFormatException {
    return FloatingDecimal.readJavaFormatString(s).doubleValue();
}

public static Double valueOf(String s) throws NumberFormatException {
    return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
}
like image 159
Michael Barker Avatar answered Oct 31 '22 02:10

Michael Barker