> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Java double

The `double` keyword in Java designates a double-precision, 64-bit IEEE 754 floating-point primitive data type. It is the default type for decimal literals in Java and provides a larger range and higher precision than the 32-bit `float` type.

## Technical Specifications

* **Memory Size:** 64 bits (8 bytes)
  * **Sign bit:** 1 bit
  * **Exponent:** 11 bits
  * **Mantissa (Significand):** 52 bits (provides 53 bits of precision due to an implicit leading bit)
* **Default Value:** `0.0d` (for instance fields, static fields, and array elements)
* **Precision:** Approximately 15 to 17 significant decimal digits
* **Minimum Non-Zero Value:** `2^(-1074)` (accessible via `Double.MIN_VALUE`)
* **Maximum Value:** `(2 - 2^(-52)) · 2^1023` (accessible via `Double.MAX_VALUE`)

## Syntax and Initialization

Because decimal literals are treated as `double` by default in Java, the `d` or `D` suffix is optional but can be used for explicit typing. `double` also supports scientific notation using `e` or `E`.

```java theme={"dark"}
// Implicit double literal
double standardValue = 123.456;

// Explicit double literal using the 'd' or 'D' suffix
double explicitValue = 123.456d;

// Scientific notation (evaluates to 12000.0)
double scientificValue = 1.2E4; 

// Hexadecimal floating-point literal (evaluates to 15.0)
double hexValue = 0x1.ep3; 
```

## Precision Limits and Rounding Errors

Because `double` represents values as base-2 fractions, it cannot exactly represent all base-10 fractions. This fundamental limitation of IEEE 754 floating-point arithmetic results in rounding errors during calculations.

```java theme={"dark"}
double a = 0.1;
double b = 0.2;
double sum = a + b; // Evaluates to 0.30000000000000004, not exactly 0.3
```

Due to these precision limits, `double` must never be used for exact calculations, such as financial or currency computations. For exact decimal arithmetic, `java.math.BigDecimal` is the required alternative.

## Comparing Double Values

Because of the aforementioned rounding errors, using the equality operator (`==`) to compare computed `double` values is a well-known anti-pattern. Comparisons of computed values should instead use an epsilon (a maximum acceptable tolerance).

The `Double.compare()` method performs an exact comparison without a tolerance, making it unsuitable for mitigating rounding errors. However, it is required for sorting and correctly handling special IEEE 754 values like `NaN` and `-0.0`.

```java theme={"dark"}
double expected = 0.3;
double actual = 0.1 + 0.2;

// Anti-pattern: Direct equality check (evaluates to false)
boolean isExact = (actual == expected); 

// Correct approach for computed values: Epsilon (tolerance) comparison
double EPSILON = 1e-9;
boolean isClose = Math.abs(actual - expected) < EPSILON; // Evaluates to true

// Double.compare() performs exact comparison (evaluates to 1, meaning not equal)
// It is used for sorting and handling special values (NaN, -0.0), not for tolerance.
int comparison = Double.compare(actual, expected);
```

## IEEE 754 Special Values

The `double` type implements standard IEEE 754 special values to handle arithmetic exceptions without throwing runtime errors. These are mapped to constants in the `java.lang.Double` wrapper class.

```java theme={"dark"}
// Double.POSITIVE_INFINITY
double positiveInfinity = 1.0 / 0.0;  

// Double.NEGATIVE_INFINITY
double negativeInfinity = -1.0 / 0.0; 

// Double.NaN (Not a Number)
double notANumber = 0.0 / 0.0;        
```

## Type Conversion

`double` is the widest primitive numeric type in Java. Conversions to `double` from other numeric primitives happen implicitly. However, while converting from `long` to `double` is an implicit widening conversion, it can result in a **loss of precision**. A `double` only has a 53-bit mantissa, meaning it cannot exactly represent all 64-bit `long` values.

```java theme={"dark"}
// Widening conversion from int (exact)
int intValue = 100000;
double widenedInt = intValue; // 100000.0

// Widening conversion from long (demonstrating loss of precision)
long largeLong = 1234567890123456789L;
double widenedLong = largeLong;          // Precision is lost in the lower bits
long convertedBack = (long) widenedLong; // Evaluates to 1234567890123456768L

// Narrowing conversion (explicit cast required)
double preciseValue = 99.999;
int narrowed = (int) preciseValue; // 99 (fractional data is truncated)
```

## The Wrapper Class

The object equivalent of the `double` primitive is `java.lang.Double`. It is used for autoboxing, generics, and provides utility methods for parsing and bit-level manipulation.

```java theme={"dark"}
// Autoboxing primitive to Wrapper
Double wrappedDouble = 42.5;

// Parsing from a String
double parsed = Double.parseDouble("3.14159");

// Bit-level representation
long bits = Double.doubleToLongBits(12.5);
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Java Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
