CKTIS / Java
util.OptionalDouble: practical reference
util.OptionalDouble: practical reference: OptionalDouble is a value-based container for one primitive double or absence, without boxing to Double.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
java.util.OptionalDoubleOptionalDouble is a value-based container for one primitive double or absence, without boxing to Double.
Use it as a numeric computation result when absence differs from 0.0, NaN, and infinity.
getAsDouble throws NoSuchElementException when empty, and the type has no generic map or flatMap; conversions must be written explicitly.
of accepts every double value; equals follows Double.compare semantics, so NaN equals NaN but +0.0 differs from -0.0.
The example exercises the documented element in a small deterministic program and compares exact standard output.
Examples
public class Main {
public static void main(String[] args) throws Exception {
var value = java.util.OptionalDouble.of(2.5);
System.out.println(value.orElse(0));
}
}
Result
2.5
The example exercises the documented element in a small deterministic program and compares exact standard output.