double

About

  • Definition: The double data type is a primitive data type in Java used to represent double-precision floating-point numbers. It provides a larger range and higher precision than float and 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-324 to 1.8E+308 for positive values (from -1.8E+308 to 1.8E+308 for negative and positive values combined), with about 15 decimal digits of precision.

  • Default Value: The default value of double is 0.0d.

  • Wrapper Class: The wrapper class for double is Double, located in java.lang.

Characteristics of double

  1. Floating-Point Representation: double is used to represent numbers that require fractional values with a high level of precision.

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

  3. Precision: double has a precision of about 15 decimal digits, which is higher than float, which only provides 6-7 decimal digits of precision. double is generally used when higher accuracy is needed, such as in scientific and financial calculations.

  4. Scientific Notation: double values can be represented in scientific notation, such as 2.34e3, which represents 2340.0.

  5. IEEE 754 Standard: double in Java follows the IEEE 754 standard for floating-point arithmetic, which includes special values like NaN (Not-a-Number), Infinity, and -Infinity.

  6. Promotion in Expressions: If a double is mixed with a float, the float is automatically promoted to a double in arithmetic expressions because double has a larger range and precision.

  7. Performance Considerations: While double provides greater precision, it can be slower in certain cases due to its larger memory footprint. However, for many modern CPUs, the performance difference between double and float is often negligible unless handling large datasets or running performance-critical code.

  8. Avoiding Precision Loss: For calculations that require higher precision (e.g., currency calculations, scientific calculations), double is typically the preferred type.

  9. Memory Usage: A double occupies 8 bytes (64 bits). The extra memory compared to float allows double to represent a much wider range of values with higher precision.

Operations with double

Arithmetic 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.3412.34f

float to double

Implicit conversion

double result = 12.34f;

Wrapper Class Example (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

  1. Loss of Precision: When performing operations involving double values, especially with large datasets or iterative processes, precision may be lost, leading to small rounding errors. This is particularly problematic for financial calculations.

  2. Comparison with NaN: NaN (Not-a-Number) is not equal to any value, including itself. Comparing NaN with double values may lead to unexpected results.

  3. Mixing double with float: When mixing double with float in expressions, the float value is promoted to a double, leading

Examples

Basic Example

Using double in Arrays

Converting double to String

Wrapper Class Example

Last updated