Skip to content
Computer ScienceISC Class 11 · Primitive Values, Wrapper Classes, Types and Casting

Type Coercion and Casting in Java (ISC Class 11 Computer Science)

Updated 17 October 2026 · Fact-checked

In a mixed expression Java automatically converts (widens) the smaller type to the larger one: byte to short to int to long to float to double. A cast written in brackets, such as (int) 3.99, forces narrowing and may lose data: the fraction is cut off (3) and values outside the range wrap around ((byte) 130 is -126). Integer division discards the fraction, so 7 / 2 is 3 but 7 / 2.0 is 3.5.

Moving up toward double is automatic and safe; moving back needs a cast and can cut off fractions or wrap values.

Illustration drawn for QuestionBuddy.

Understand Automatic Type Coercion (Widening) and Explicit Casting (Narrowing)

Widening (implicit, coercion). Order: byte, short, int, long, float, double (char goes up to int). In a binary operation both operands are converted to the larger type; byte, short and char operands become int. Widening is safe (no loss of range).

  • int + double gives double; int + long gives long; char + int gives int.
  • 7 / 2 is 3 (both int: integer division); 7 / 2.0 is 3.5; 7 % 2 is 1.

Narrowing (explicit cast). (type) expression. May lose data.

  • (int) 3.99 is 3 (truncation, not rounding); (int) -9.99 is -9.
  • (byte) 130 is 130 - 256 = -126; (byte) 200 is -56.
  • (char) 66 is 'B'.
  • Math.round(9.5) is 10 (round half up), but (int) 9.5 is 9.

Assignment. int x = 3.5; is a compile-time error (possible loss of precision); write int x = (int) 3.5;. float f = 3.5; is an error because 3.5 is a double; write 3.5f. Widening on assignment is automatic: double d = 7; gives 7.0.

Casting position. Cast before dividing: (double) a / b gives a real result; (double) (a / b) divides first and then converts, so 7/2 gives 3.0.

Why avoid narrowing. It can silently lose the fraction or wrap the value, giving wrong results; use it only when needed and when the value is known to fit.

Integer overflow in mixed arithmetic. int x int is computed as int even if stored in a long: 1000000 * 1000000 gives -727379968; write 1000000L * 1000000 to get 1000000000000.

Automatic Type Coercion (Widening) and Explicit Casting (Narrowing): key facts

Widening order
byte -> short -> int -> long -> float -> double | char -> int
Automatic.
Casts
  • (int) 3.99 = 3
  • (byte) 130 = -126
  • (char) 66 = B
Narrowing may lose data.
Division types
int / int = int (fraction dropped) | int / double = double
7/2 = 3; 7/2.0 = 3.5.

How to answer Automatic Type Coercion (Widening) and Explicit Casting (Narrowing) questions

Work out the type of each sub-expression from left to right.

  1. 1Write the type of every operand.
  2. 2For each operator, widen to the larger type.
  3. 3Apply integer division when both operands are int.
  4. 4Apply casts in the position written; narrowing truncates or wraps.

Automatic Type Coercion (Widening) and Explicit Casting (Narrowing) in real life

The idea, met in an everyday situation. Answer the question in your own words, then compare with the model answer. The one-line hook is what to remember.

Average of marks

Suppose a program stores the total marks 238 and the number of subjects 3 as ints and prints the average as total / n.

Is the result exact and how to fix it?

Try it first, then show the solutionSolution
  1. 238 / 3 with both operands int gives 79 (integer division), so the 0.33 is lost.
  2. Use (double) total / n to get 79.33.

Answer: Integer division loses the fraction; cast one operand to double.

Memory hook: Cast first, divide second.

Tricks to remember Automatic Type Coercion (Widening) and Explicit Casting (Narrowing)

Short memory aids. Say each one aloud once, then use it on a question the same day.

  • Small to big is safe

    Widening is automatic.

    Use it when: Coercion.

  • Cut, not round

    (int) removes the decimal part.

    Use it when: Casting.

  • Both int means int

    int / int stays int.

    Use it when: Division.

Automatic Type Coercion (Widening) and Explicit Casting (Narrowing): solved examples

Ordered from board level to HOTS. Try each one before you open the solution.

Example 1: Integer division · 1 mark · MCQBoard level

The value of 7 / 2 in Java is:
(a) 3.5 (b) 3 (c) 4 (d) 3.0

Try it first, then show the solutionSolution
  1. Both operands are int: integer division gives 3.

Answer: (b) 3

Example 2: Output tracing · 3 marks · Short answerBoard level

Give the output:

int a = 7, b = 2;
double d = a / b;
double e = (double) a / b;
int p = (int) 9.99;
int q = (int) -9.99;
System.out.println(d + " " + e + " " + p + " " + q);

(3 marks)

Try it first, then show the solutionSolution
  1. a / b = 7 / 2 = 3 (int) then widened to 3.0.
  2. (double) a / b = 7.0 / 2 = 3.5.
  3. (int) 9.99 = 9; (int) -9.99 = -9.

Answer: 3.0 3.5 9 -9

Example 3: Mixed expressions · 7 marks · Short answerHarder board level

Find the value and type: (i) 5 / 2 * 2 (ii) 5 / 2 * 2.0 (iii) 5 * 2 / 4 (iv) 5 * 2.0 / 4 (v) 'a' + 1 (vi) (char) ('a' + 1) (vii) 10 % 4 + 10 / 4.0. (7 marks)

Try it first, then show the solutionSolution
  1. (i) 5 / 2 = 2; 2 x 2 = 4 (int).
  2. (ii) 5 / 2 = 2; 2 x 2.0 = 4.0 (double).
  3. (iii) 10 / 4 = 2 (int).
  4. (iv) 10.0 / 4 = 2.5 (double).
  5. (v) 97 + 1 = 98 (int).
  6. (vi) b (char).
  7. (vii) 10 % 4 = 2; 10 / 4.0 = 2.5; sum 4.5 (double).

Answer: (i) 4 int; (ii) 4.0 double; (iii) 2 int; (iv) 2.5 double; (v) 98 int; (vi) b char; (vii) 4.5 double.

Example 4: Casting and wrap-around · 6 marks · Long answerHarder board level

Find: (byte) 130, (byte) 200, (int) 'B' + 1, (char) 100, (int) Math.round(9.5), (int) 9.5 and the result of int x = 1000000 * 1000000 given that the product is computed as int (answer from 10^12 mod 2^32 interpreted as signed). (6 marks)

Try it first, then show the solutionSolution
  1. (byte) 130 = 130 - 256 = -126; (byte) 200 = -56.
  2. (int) 'B' + 1 = 66 + 1 = 67.
  3. (char) 100 = 'd'.
  4. Math.round(9.5) = 10; (int) 9.5 = 9.
  5. 10^12 = 232 x 2^32 + 3,567,587,328; as a signed int, 3,567,587,328 - 4,294,967,296 = -727,379,968.

Answer: -126, -56, 67, d, 10, 9 and -727379968

Example 5: Fix the program · 5 marks · Long answerHOTS / competency

A program computes the percentage as int marks = 463, total = 600; int pct = marks * 100 / total; double avg = marks / 6; and the student expects 77.17 and 77.17. Find the actual values, explain the loss and rewrite correctly. (5 marks)

Try it first, then show the solutionSolution
  1. marks * 100 / total = 46300 / 600 = 77 (int); the .17 is lost.
  2. marks / 6 = 463 / 6 = 77 (int division) then stored as 77.0.
  3. Rewrite: double pct = marks * 100.0 / total; double avg = marks / 6.0; giving 77.1666... and 77.1666...
  4. A cast placed after the division, (double) (marks / 6), would still give 77.0.

Answer: pct = 77 and avg = 77.0 (fractions lost). Use marks * 100.0 / total and marks / 6.0 to get 77.166...

Where students lose marks in Automatic Type Coercion (Widening) and Explicit Casting (Narrowing)

The slips examiners see most often, and what to write instead.

  • Mistake: Rounding in (int) instead of truncating

    Maths habit.

    Wrong working

    (int) 9.99 = 10

    Right working

    9

    Do this: Cast truncates.

    Usually costs: 2 marks.

  • Mistake: Casting after the division

    Position error.

    Wrong working

    (double) (a / b) = 3.5

    Right working

    3.0

    Do this: Cast an operand first.

    Usually costs: 2 marks.

  • Mistake: Forgetting that char + int is int

    Printing a character.

    Wrong working

    System.out.println('a' + 1) gives b

    Right working

    98

    Do this: Cast to char for b.

    Usually costs: 1-2 marks.

Exam tips

  • Write the type of each sub-expression in tracing questions.
  • State that integer division discards the fraction.
  • Explain data loss when asked why casting is avoided.

Practice questions on Automatic Type Coercion (Widening) and Explicit Casting (Narrowing)

Write each answer on paper first. Open the hint only if you are stuck, and the answer only when you have finished.

  1. Q1 · 1 mark · MCQBoard level

    (int) 8.99 gives:
    (a) 9 (b) 8 (c) 8.99 (d) 8.0

    Hint

    Truncates.

    Answer

    (b) 8

  2. Q2 · 2 marks · Short answerBoard level

    Distinguish between implicit and explicit type conversion with an example of each. (2 marks)

    Hint

    Automatic vs cast.

    Answer

    Implicit (coercion) is done by the compiler, such as double d = 5; (5.0). Explicit conversion is written as a cast, such as int n = (int) 5.7; (5).

  3. Q3 · 3 marks · Short answerHarder board level

    Find the output: int a = 9, b = 4; System.out.println(a / b + " " + a % b + " " + (double) a / b + " " + (double) (a / b) + " " + a / (double) b); (3 marks)

    Hint

    9 / 4 = 2.

    Answer

    2 1 2.25 2.0 2.25

  4. Q4 · 4 marks · Short answerHarder board level

    Find the value of x after each: double x = 7 / 2; x = x + 7 / 2.0; x = (int) x + 2; and state its final value and type (declare x as double). (4 marks)

    Hint

    3.0, then 6.5.

    Answer

    x = 7 / 2 = 3 widened to 3.0; x = 3.0 + 3.5 = 6.5; x = (int) 6.5 + 2 = 6 + 2 = 8, stored as 8.0 (double).

  5. Q5 · 3 marks · Short answerHOTS / competency

    A shop program converts rupees to dollars using int dollars = rupees / 80; with rupees = 7900 (int) and prints 98. A manager complains that the total for 1,000 customers is out by a large amount. Explain the cause and the fix. (3 marks)

    Hint

    Fractions lost each time.

    Answer

    7900 / 80 = 98.75 but int division gives 98, losing 0.75 per customer, which adds up (about 750 dollars over 1,000 customers). Use double dollars = rupees / 80.0; and sum the doubles.

Test your recall

Answer in your head or on paper first, then open the card. Pulling an answer out of memory makes it stick far better than re-reading.

  1. 1.7/2 in Java?Show answer

    3.

  2. 2.(int) 9.99?Show answer

    9.

  3. 3.(byte) 130?Show answer

    -126.

  4. 4.char + int is?Show answer

    int.

  5. 5.Widening order ends at?Show answer

    double.

Automatic Type Coercion (Widening) and Explicit Casting (Narrowing): frequently asked questions

Does casting round?

No, it truncates.

Is float f = 3.5 valid?

No, use 3.5f.

Is widening safe?

Generally yes.

What does Math.round return?

The nearest long or int, half up.