Swipe
SwipeSwipe
Swipe Apps

Paths

Motivation

Information

Constants & Type Casting · Lesson 28 of 28

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

🤖
Converting between types isn't always free — some directions are riskier. 🔄

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
Widening happens automatically because every int value fits perfectly inside a double.
🤖
Explicit casts are Java's way of making you own the risk. ✍️

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
Casting a double to int truncates (cuts off) the decimal part — it does not round.
🤖
Overflow bugs are sneaky — the number doesn't error, it just lies. 🐛

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
Casting a value too large to fit into int causes it to overflow instead of simply being cut down.
Fill in the blank

Converting a larger type into a smaller one, such as double to int, is called ____ casting and requires an explicit cast.

Quick check

What does casting the double 9.99 to an int produce?

True or false

Widening a value, such as converting an int to a double, happens automatically in Java.

Chapter quiz

Constants & Type Casting

10 questions · pass with 60% — just like in the app.

Quick check

What does the final keyword do when applied to a variable?

True or false

Constant names in Java conventionally use UPPER_SNAKE_CASE, like MAX_SPEED.

Fill in the blank

Trying to reassign a final variable causes a ____ error.

Quick check

Why are constants useful in code?

True or false

Converting an int to a double happens automatically in Java because no data is lost.

Fill in the blank

Converting a larger type into a smaller one, such as double to int, is called ____ casting.

Quick check

What does casting the double 9.99 to an int produce?

True or false

Explicit (narrowing) casting requires you to write the target type in parentheses before the value.

Fill in the blank

Casting a value that is too large to fit into a smaller type can cause the value to ____.

Quick check

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 →