Course contentsShow
← Java
Lesson 89 of 2,8703. Operators and ExpressionsFree lesson

Mixing int and double in Arithmetic Expressions

Learn how Java promotes int to double when mixing types, producing floating-point results.

Mixing int and double in Arithmetic Expressions

What you'll learn: How Java automatically converts int to double when these types appear together in calculations, ensuring you get decimal results.


The Core Rule: Automatic Promotion

When you mix int and double in the same arithmetic expression, Java automatically promotes the int to a double before performing the operation. The result is always a double.

Think of it like mixing liquids: if you pour water (int) into juice (double), everything becomes juice. Java "upgrades" the simpler type to match the more precise one.

Why This Happens

Java follows a promotion hierarchy. Since double can represent more values than int (including fractions), Java converts the int to double to avoid losing information. This happens automatically—no cast required.

Examples

int wholeNumber = 5;
double decimal = 2.0;
double result = wholeNumber + decimal;  // 5 is promoted to 5.0, result is 7.0
double answer = 10 / 4.0;  // 10 becomes 10.0, answer is 2.5 (not 2!)
int a = 7;
double b = 3.5;
double product = a * b;  // 7 becomes 7.0, product is 24.5

The Practical Impact

This automatic promotion helps you avoid integer division pitfalls. If you write 10 / 4.0 instead of 10 / 4, you get 2.5 instead of 2 because the presence of 4.0 forces the entire calculation into floating-point mode.

Key Takeaway: When you mix int and double in arithmetic, Java promotes the int to double automatically, and the result is always a double—giving you decimal precision instead of truncated integer results.