Java int to double: Conversion Rules and Precision
java int to double: Learn how Java converts int to double through widening conversion, when explicit casting is required, and where precision can be lost in arithmetic.
java int to double requires a clear understanding of the core syntax, runtime behavior, and practical implementation patterns demonstrated in the examples below.
In Java, converting an int to a double is a widening primitive conversion, which means the compiler handles it automatically in most cases. Because every int value fits exactly within the range and precision of a double, the conversion never truncates or rounds the original value. The practical challenge is not the conversion itself but knowing where Java applies it implicitly and where you must request it explicitly, particularly in arithmetic expressions.
Automatic Widening Conversion
When you assign an int to a double variable, no cast is required:
int count = 42; double total = count;
The assignment compiles because double has a wider range and a larger significand than int. Java defines this as a widening primitive conversion, and the compiler inserts the conversion without any runtime cost. The same rule applies when an int is passed to a method that accepts a double parameter:
public void recordPrice(double price) { // ... } int basePrice = 199; recordPrice(basePrice);
The int argument is widened to double before the method body executes. This is the most common way the conversion appears in real code, and it requires no special handling.
Explicit Casting
A cast is necessary when the conversion would otherwise be hidden by Java's arithmetic rules. The clearest example is division. When both operands are int, Java performs integer division and discards the fractional part:
int numerator = 7; int denominator = 2; double result = numerator / denominator; // result is 3.0, not 3.5
The assignment to double happens after the integer division completes, so the fractional part is already lost. Casting one operand to double changes the arithmetic itself:
double result = (double) numerator / denominator; // result is 3.5
The cast applies only to numerator, which forces the division to use floating-point semantics. Casting the denominator instead produces the same result. This pattern is the main reason developers search for java int to double: they expect a fractional quotient and receive a truncated integer.
Using the Double Wrapper
The Double class offers a static factory method that accepts an int:
Double wrapped = Double.valueOf(42);
This returns a Double object rather than a primitive double. It is useful when you need to store the value in a generic collection such as List<Double> or when an API requires a boxed value. For primitive arithmetic, the wrapper adds no benefit and introduces object allocation. The widening conversion already covers every case where a primitive double is needed, so prefer the direct assignment or cast unless the boxed type is explicitly required.
| Conversion approach | Result type | When to use |
|---|---|---|
| Direct assignment | double | Default choice for primitive values |
| Cast in expression | double | Force floating-point arithmetic |
Double.valueOf(int) | Double | Need a boxed object for collections or generics |
Precision and Large Values
An int is a 32-bit signed integer with a maximum value of 2,147,483,647. A double stores a 52-bit significand plus an 11-bit exponent, so it can represent every int exactly. The conversion from int to double therefore never loses information at the point of conversion.
Precision issues appear only after the conversion, when you perform arithmetic that produces values with more significant digits than the significand can hold, or when you compare a double result against an int constant. For example, adding a very small double to a large double may round the result because the significand cannot represent the difference. This is a property of floating-point arithmetic, not of the widening conversion itself.
If you need exact decimal arithmetic after conversion, for example in financial calculations, use BigDecimal instead of double. The conversion from int to double is safe, but the subsequent operations may not be.
Division Behavior in Practice
The most frequent production scenario is computing a ratio or percentage from two integer counts. Consider a metrics system that tracks successful requests:
int succeeded = 812; int total = 1000; double successRate = (double) succeeded / total * 100;
Without the cast, succeeded / total evaluates to 0, and the percentage becomes 0.0. The cast on the first operand changes the entire expression to floating-point evaluation. An alternative is to multiply by 1.0:
double successRate = succeeded * 1.0 / total * 100;
Both forms are equivalent. The cast is more explicit about intent, while the 1.0 multiplication is a common idiom in codebases that prefer to avoid casts. Choose one style and apply it consistently within a project.
Performance Considerations
The widening conversion from int to double has no measurable runtime cost. It is a register-level operation that the JVM handles without allocation, method calls, or branching. Casting in an arithmetic expression is equally cheap because the JIT compiler treats the cast as a type annotation rather than a runtime operation.
The performance concern that does exist is boxing. Converting an int to a Double object allocates on the heap, and doing so inside a hot loop creates avoidable garbage. If you only need primitive arithmetic, stay with the primitive double and avoid Double.valueOf or autoboxing. When a collection of boxed values is unavoidable, prefer primitive-specialized libraries or arrays of double for performance-sensitive paths.
The conversion itself is never a bottleneck. The surrounding arithmetic, allocation, and collection usage determine whether the code performs well.