I have to convert Strings to Doubles, but may be I have a null value. Does it work with Double.parseDouble(stringValue)
or Double.valueOf(stringValue)
?
What is the difference between this two methods?
Thank you in advance.
Neither method makes any difference. Both will throw a NullPointerExecption
. Instead, you need to add a helper method which returns a Double
e.g.
static Double parseDouble(String s) {
return s == null ? null : Double.parseDouble(s);
}
or you can return a double
by having a default value
static double parseDouble(String s, double otherwise) {
return s == null ? otherwise : Double.parseDouble(s);
}
a suitable value might be Double.NaN
static double parseDouble(String s) {
return s == null ? Double.NaN : Double.parseDouble(s);
}