Type Casting
A free lesson from Swipe Coding · Java, level 2 · updated October 2026
By the Swipe team, founded by Colin · how we make lessons
Type casting means converting a value from one type to another. Java has two kinds: implicit (widening) casting, which happens automatically when no data can be lost, and explicit (narrowing) casting, which you must write yourself because it risks losing information.
int wholeNumber = 10; double decimalNumber = wholeNumber; // implicit widening System.out.println(decimalNumber); // 10.0
Going the other way — from a larger or more precise type to a smaller one, like double to int — is called narrowing, and Java requires you to write an explicit cast: the target type in parentheses right before the value. This forces you to acknowledge that some information might be lost.
double price = 9.99; int wholePrice = (int) price; System.out.println(wholePrice); // 9
Precision loss isn't limited to decimals: casting a whole-number type into a smaller one can also lose data if the value doesn't fit. When that happens, the number can "wrap around" into a completely unexpected — even negative — result, which is a common source of subtle bugs.
long big = 3000000000L; int smaller = (int) big; System.out.println(smaller); // overflows to an unexpected, incorrect number
Converting a larger type into a smaller one, such as double to int, is called ____ casting and requires an explicit cast.
What does casting the double 9.99 to an int produce?
Widening a value, such as converting an int to a double, happens automatically in Java.
Constants & Type Casting
10 questions · pass with 60% — just like in the app.
What does the final keyword do when applied to a variable?
Constant names in Java conventionally use UPPER_SNAKE_CASE, like MAX_SPEED.
Trying to reassign a final variable causes a ____ error.
Why are constants useful in code?
Converting an int to a double happens automatically in Java because no data is lost.
Converting a larger type into a smaller one, such as double to int, is called ____ casting.
What does casting the double 9.99 to an int produce?
Explicit (narrowing) casting requires you to write the target type in parentheses before the value.
Casting a value that is too large to fit into a smaller type can cause the value to ____.
Which of these is an example of implicit (widening) casting?
Keep your streak going in Swipe Coding
Swipe through every level of Java on your iPhone — with XP, stars, streaks, a personal advisor and the full track beyond these free levels.
Get the Swipe apps →