Stop 02 · Variables, Data Types & OperatorsTopics · 4/7

Type Casting & Promotion

Part of the Java Roadmap

In short

Mixing an int with a double, or squeezing a decimal into an int, happens all the time. Java quietly handles the safe direction and makes you say "I know what I am doing" for the risky one.

Widening conversions (int to long or double) happen automatically, while narrowing conversions (double to int) need an explicit cast and may lose data.

The jaadu pours chai from a large mug into a tiny clay cup, and some of it spills over the edge onto a saucer.

Think of it like this

Pouring between glasses

Pour a small cup of chai into a big mug and nothing spills. That is widening: int to long or double, done automatically. Pour a big mug into a small cup and some will overflow. Java makes you do that yourself, with a cast, so you own the spill.

Watch it happen

Widening happens automatically, left to right

byte
short
int
long
float
double

1/5A byte can move right into any bigger type with no cast.

The big ideas

What you need to know

Widening is automatic

int to long, long to double and so on. No cast needed because nothing important is lost.

Narrowing needs a cast

(int) 9.99 is 9. The decimal part is cut off, not rounded. Use Math.round for rounding.

Promotion in expressions

byte, short and char become int in arithmetic. In a mix, the result takes the widest type.

Overflow is silent

(byte) 200 becomes -56. Casting to a smaller type can wrap around without any error.

See it in code

Walk through the code

java
1public class Casting {2    public static void main(String[] args) {3        int marks = 437;4        int total = 500;5 6        double percent = (double) marks / total * 100;7        System.out.println("Percent: " + percent);8 9        int rounded = (int) Math.round(percent);10        int truncated = (int) percent;11        System.out.println(rounded + " vs " + truncated);12 13        long big = marks;          // widening: automatic14        byte small = (byte) 200;   // narrowing: wraps around15        System.out.println(big + " " + small);16 17        char c = 'A';18        System.out.println(c + 1);          // 6619        System.out.println((char) (c + 1)); // B20    }21}

1/4The cast applies to marks first, so the division is done in double: 87.4.

Casting before dividing, rounding vs truncating, and what char arithmetic really does.

Save yourself the pain

Common mistakes

Oops

double percent = (double) (marks / total) * 100;

Do this instead

The brackets make integer division happen first, giving 0. Cast one operand: (double) marks / total * 100.

Oops

Using (int) to round a price.

Do this instead

(int) 99.9 is 99. Use Math.round(99.9), which returns 100 as a long.

Your mission

Percentage calculator

~15 min

Quick check

What is the value of (int) 7.9?

Go deeper

  1. 1Cast a double to an int and observe the truncation.
  2. 2Convert strings to numbers with Integer.parseInt() and Double.parseDouble().
  3. 3See what happens when you cast a large long to an int.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge