HOWTO · Java
Double Division in Java
Learn why Java integer division drops the fractional part and how casting or mixing numeric types produces a floating-point quotient.
On this page
To get a fractional quotient in Java, make at least one operand a floating-point value before the / operation. Dividing two int values uses integer division and discards the fractional part; assigning that result to a double afterward cannot bring the discarded fraction back. Casting one operand with (double) before division is enough, and Java promotes the other operand for that operation without changing its variable.
Understanding Integer Division in Java
When both operands are integral types, Java performs integer division. The quotient is rounded toward zero, so 30 / 8 evaluates to 3, not 3.75. This happens in the expression itself, before assignment or printing. Use integer division when the fractional part is intentionally unwanted, such as when calculating a whole-number count.
Getting a Double Result from Integer Operands
Cast either operand before / so the division is floating-point. The cast changes the type of that operand expression; binary numeric promotion then converts the other operand for this operation. The original int variable remains an int.
Run this complete example:
public class DivisionExample {
public static void main(String[] args) {
int numerator = 30;
int denominator = 8;
double quotient = (double) numerator / denominator;
System.out.println("Integer division: " + (numerator / denominator));
System.out.println("Cast before division: " + quotient);
System.out.println("Mixed operands: " + (numerator / 8.0));
System.out.println("Variable type remains int: " + (numerator));
}
}
Integer division: 3
Cast before division: 3.75
Mixed operands: 3.75
Variable type remains int: 30
The first line uses integer division. Casting before / and using the 8.0 literal produce fractional results, while numerator remains an int.
Dividing Mixed Numeric Types
If one operand is double and the other is int, Java promotes the int operand to double for the division, and the expression has type double. For example, 30 / 8.0 produces 3.75; 30 remains an integer value in its original variable. A float operand also makes the operation floating-point, but the result type follows binary numeric promotion: with float and int operands it is float, while combining float and double produces double.
Common Division Pitfalls
- Casting after division is too late.
(double) (30 / 8)casts the already computed integer3, yielding3.0. Cast first:(double) 30 / 8. - The destination type does not control the operation.
double quotient = 30 / 8;still computes integer division first, soquotientreceives3.0. - Choose
floatordoubledeliberately.doublehas greater precision thanfloat; both use binary floating-point and can approximate some decimal fractions. For many decimal-place outputs, format the value when displaying it. - Division by zero depends on operand types. Integer division by zero throws
ArithmeticException. Floating-point division by zero follows IEEE 754: a nonzero value divided by0.0yields an infinity, while0.0 / 0.0yieldsNaN.
FAQ
What does Java return when dividing two int values?
The division expression has an integral result, with any fractional part discarded by rounding toward zero. For example, 30 / 8 is 3.
How do I get a double result from integer values?
Cast one operand before division, as in (double) numerator / denominator, or use a floating-point literal for one operand.
Can I divide an int by a double?
Yes. Java promotes the int for the operation, and the expression result is double; the original variable’s declared type does not change.
What is the difference between float and double?
float is a 32-bit IEEE 754 binary floating-point type, while double is 64-bit. double generally provides more precision and range, but neither represents every decimal fraction exactly.