double
About
Definition: The
doubledata type is a primitive data type in Java used to represent double-precision floating-point numbers. It provides a larger range and higher precision thanfloatand is commonly used for calculations that require higher accuracy, such as scientific computations.Size: Occupies 8 bytes (64 bits) in memory.
Value Range: From approximately
4.9E-324to1.8E+308for positive values (from-1.8E+308to1.8E+308for negative and positive values combined), with about 15 decimal digits of precision.Default Value: The default value of
doubleis0.0d.Wrapper Class: The wrapper class for
doubleisDouble, located injava.lang.
Characteristics of double
doubleFloating-Point Representation:
doubleis used to represent numbers that require fractional values with a high level of precision.Memory Usage: It occupies 8 bytes (64 bits) of memory, which is larger than
float, making it suitable for more precise calculations but at the cost of increased memory usage.Precision:
doublehas a precision of about 15 decimal digits, which is higher thanfloat, which only provides 6-7 decimal digits of precision.doubleis generally used when higher accuracy is needed, such as in scientific and financial calculations.Scientific Notation:
doublevalues can be represented in scientific notation, such as2.34e3, which represents2340.0.IEEE 754 Standard:
doublein Java follows the IEEE 754 standard for floating-point arithmetic, which includes special values likeNaN(Not-a-Number),Infinity, and-Infinity.Promotion in Expressions: If a
doubleis mixed with afloat, thefloatis automatically promoted to adoublein arithmetic expressions becausedoublehas a larger range and precision.Performance Considerations: While
doubleprovides greater precision, it can be slower in certain cases due to its larger memory footprint. However, for many modern CPUs, the performance difference betweendoubleandfloatis often negligible unless handling large datasets or running performance-critical code.Avoiding Precision Loss: For calculations that require higher precision (e.g., currency calculations, scientific calculations),
doubleis typically the preferred type.Memory Usage: A
doubleoccupies 8 bytes (64 bits). The extra memory compared tofloatallowsdoubleto represent a much wider range of values with higher precision.
Operations with double
doubleArithmetic and Logical Operations
Operation
Example
Description
Arithmetic Operations
double sum = a + b;
Addition, subtraction, multiplication, division.
Comparison Operations
a == b, a > b, etc.
Compares two double values.
Bitwise Operations
Not supported directly on double
double does not support bitwise operators like &, `
Conversion Methods
Conversion
Method
Example
double to String
String.valueOf(double)
String.valueOf(12.34) → "12.34"
String to double
Double.parseDouble(String)
Double.parseDouble("12.34") → 12.34
double to float
Explicit cast (float)
(float) 12.34 → 12.34f
float to double
Implicit conversion
double result = 12.34f;
Wrapper Class Example (Double)
Double)Method
Description
Double.valueOf(double d)
Returns a Double object for the given double value.
Double.parseDouble(String s)
Parses the string argument as a double.
Double.toString(double d)
Converts double to its String representation.
Double.isNaN(double d)
Checks if the double value is NaN.
Double.isInfinite(double d)
Checks if the double value is positive or negative infinity.
Common Mistakes
Loss of Precision: When performing operations involving
doublevalues, especially with large datasets or iterative processes, precision may be lost, leading to small rounding errors. This is particularly problematic for financial calculations.Comparison with
NaN:NaN(Not-a-Number) is not equal to any value, including itself. ComparingNaNwithdoublevalues may lead to unexpected results.Mixing
doublewithfloat: When mixingdoublewithfloatin expressions, thefloatvalue is promoted to adouble, leading
Examples
Basic Example
Using double in Arrays
double in ArraysConverting double to String
double to StringWrapper Class Example
Last updated