Learn Java
import com.learnjava.variablesdatatypes.TypeCasting;

Type Casting (Widening & Narrowing) in Java

@Concept

Casting converts a value from one type to another. Widening happens automatically whenever no information could be lost β€” an int assigned to a double, for instance, since every int value fits inside double's much larger range. Narrowing goes the other direction, from a larger or more precise type into a smaller one, and Java requires it to be written explicitly, with the target type in parentheses, precisely because it can lose information.

That explicit syntax is a safety rail: writing double d = 5; compiles without comment, but int i = d; does not β€” the compiler forces int i = (int) d; instead, so truncation is something the code's author visibly chose rather than something that happened silently. A narrowing cast can lose the fractional part of a decimal or wrap a value that's outside the target type's range, exactly like the overflow behavior primitive types already show.

@HowItWorks
1

Widening

A smaller or less precise type converts automatically into a larger, more precise one: byte to short to int to long to float to double, with no cast needed.

2

Narrowing

A larger or more precise type converts into a smaller one only with an explicit cast β€” the target type written in parentheses in front of the value.

3

Why the compiler demands it

Narrowing can lose information β€” a fractional part, or magnitude outside the new type's range β€” so Java requires the cast to be visible in the source rather than letting it happen silently.

4

Truncation, not rounding

Casting a double to an int simply chops off everything after the decimal point; it does not round to the nearest whole number.

5

Casting between unrelated reference types

Casting one object reference type to another, unlike primitive casting, only works if the object is genuinely an instance of the target type β€” Java checks that at runtime and throws a ClassCastException if it isn't.

@Example
public class CastingDemo {
public static void main(String[] args) {
int wholeNumber = 9;
double widened = wholeNumber;
double preciseValue = 9.75;
int narrowed = (int) preciseValue;
System.out.println("Widened: " + widened);
System.out.println("Narrowed: " + narrowed);
}
}
Output
Widened: 9.0
Narrowed: 9
1double widened = wholeNumber; needs no cast, since every int value fits safely inside a double.
2preciseValue holds 9.75, a value an int cannot represent exactly.
3(int) preciseValue explicitly narrows it, truncating β€” not rounding β€” down to 9.
4The two println calls print once each, showing the widened value keeping its decimal point and the narrowed value losing its fractional part entirely.
@UseCases
Converting a value parsed as a double into a whole-number count without an unexpected compiler error
Understanding why casting 9.99 to an int gives 9, not 10
Debugging a ClassCastException thrown by an invalid cast between unrelated object types
Deciding whether a calculation needs an explicit cast to avoid losing precision partway through

Recommended resourcesAffiliate links Β· may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice β€” pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from β€” great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself β€” building beats watching.