What is Double.parseDouble(String) and what is the difference between Double.parseDouble(String) and Double.valueOf(double)?
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.
Whats the difference between Double.valueOf(String s) and Double.ParseDouble(String s)?
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());
}
Double.valueOf(s) vs. Double.parseDouble
parseDouble()
returns a primitive double
value. valueOf()
returns an instance of the wrapper class Double
. Before Java 5 introduced autoboxing, that was a very significant difference (and many would argue it still is).
What is the difference between Double.parseDouble(String) and Double.valueOf(String)?
parseDouble
returns a primitive double containing the value of the string:
Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.
valueOf
returns a Double instance, if already cached, you'll get the same cached instance.
Returns a Double instance representing the specified double value. If
a new Double instance is not required, this method should generally be
used in preference to the constructor Double(double), as this method
is likely to yield significantly better space and time performance by
caching frequently requested values.
To avoid the overhead of creating a new Double object instance, you should normally use valueOf
What's the difference between calling Double.valueOf(String s) and new Double(String s)?
Depends on the implementation. openJDK 6 b14 uses this implementation of Double(String s)
:
this(valueOf(s).doubleValue());
So it calls valueOf(String s)
internally and must be less efficient compared to calling that method directly.
Difference in behaviour between Double.parseDouble and Integer.parseInt
This behaviour is actually documented (though this is quite a bad design...)!
Double.parseDouble
:
Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of class Double.
Double.valueOf
:
Leading and trailing whitespace characters in s are ignored. Whitespace is removed as if by the String.trim() method; that is, both ASCII space and control characters are removed.
Integer.parseInt
:
The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value.
String.format(%.2f,double) works only in half of the tests
A double
(or its boxed counterpart Double
) has no capacity to store a specific format to be used when rendering as a String. It is just a raw value.
I suspect you believe that a double
value obtained by parsing from a specific format somehow retains that format. This is not the case.
Floating point types are imprecise; not all values can be accurately stored. In cases where the exact value cannot be stored, the closest storable value is used - as is the case with some of your results.
What is the difference between new Double(someString) and Double.parseDouble(someString)
One returns Double
; the other, double
.
The differences between primitive Java types and their wrapper counterparts are discussed, for example, here.
difference between Double.valueOf and doubleValue
Double.valueOf
returns a Double
type, then calling doubleValue()
gives you primitive double type. You can replace both the methods with Double.parseDouble
double displayNumber = Double.valueOf(display.getText()).doubleValue()
to
double displayNumber = Double.parseDouble(display.getText())
Related Topics
How to Make Lombok and Aspectj Work Together
How to Create Change Listener for Variable
In Java, How to Efficiently and Elegantly Stream a Tree Node's Descendants
Best Way to Get Geo-Location in Java
How to Parse Month Full Form String Using Dateformat in Java
How to Use "Cd" Command Using Java Runtime
Simpledateformat Parse Loses Timezone
Count Words in a String Method
How to Get a Value from the Last Inserted Row
Spring Qualifier and Property Placeholder
What Is the Easiest Way to Ignore a JPA Field During Persistence
How to Read Request Body Multiple Times in Spring 'Handlermethodargumentresolver'
Java.Util.Zip - Recreating Directory Structure