Casting & Range

Mastering data conversion, navigating the limits of memory, and writing efficient shortcuts.

🥤 Too Much for the Cup

When a big drink meets a small cup, something has to be left behind.

⚠️ The Problem

Why can't we just assign a double to an int?

HeroDisplay.java

Java is strict

A double holds more information than an int. Java refuses to silently lose that data.

The solution: Cast

Use an explicit cast to tell Java that the possible data loss is intentional.

🔄 Casting

Changing types: Narrowing vs. Widening

Narrowing

Convert a larger type to a smaller type.

double → int requires an explicit cast because data may be lost.

Widening

Convert a smaller type to a larger type.

int → double happens automatically because the value remains representable.

double
99.9
needs (int)
→
int
99
int
5
automatic
→
double
5.0

Syntax

Use (int) someDouble to narrow or (double) someInt to widen explicitly.

🔁 Two Type Conversions

Compare what Java requires when values move between int and double.

Narrowing: double → int

Narrowing.java

Requires an explicit cast because the decimal part may be lost.

Widening: int → double

Widening.java

Happens automatically because no integer information is lost.

🧭 Evaluation Order

Casting and arithmetic happen before the final assignment.

EvaluationOrder.java

1. Cast

Evaluate (int) value first. The value becomes 9.

2. Arithmetic

Then calculate 9 × 2. The result becomes 18.

3. Assignment

Finally, store 18 in result.

Parentheses can change the order: (int)(value * 2) multiplies first,then casts the result to 19.

✂️ Truncation Rules

Casting cuts the decimal — it never rounds

CastingDemo.java

99.9 → 99

The decimal portion is dropped, not rounded.

3.9 → 3

Even 3.9 becomes 3.

Truncation always drops the fractional part.

-2.9 → -2

For negative values, dropping the decimal moves the result toward zero.

🔍 Casting Quiz (1/10)

What value is printed?

CastingQuiz.java

🔍 Casting Quiz (2/10)

What value is printed?

CastingQuiz.java

🔍 Casting Quiz (3/10)

What value is printed?

CastingQuiz.java

🔍 Casting Quiz (4/10)

What value is printed?

CastingQuiz.java

🔍 Casting Quiz (5/10)

What value is printed?

CastingQuiz.java

🔍 Casting Quiz (6/10)

What value is printed?

CastingQuiz.java

🔍 Casting Quiz (7/10)

What value is printed?

CastingQuiz.java

🔍 Casting Quiz (8/10)

What value is printed?

CastingQuiz.java

🔍 Casting Quiz (9/10)

What value is printed?

CastingQuiz.java

🔍 Casting Quiz (10/10)

What value is printed?

CastingQuiz.java

🎯 Rounding Strategy

How to achieve (nearest int) instead of just truncation

Positive Numbers

Add 0.5 before casting.

Pattern: (int)(x + 0.5)

Negative Numbers

Subtract 0.5 before casting.

Pattern: (int)(x - 0.5)

Positive Example

8.68.6 + 0.59.1
Offset
(int) 9.19
Truncate result

Negative Example

-8.6-8.6 - 0.5-9.1
Offset
(int) -9.1-9
Truncate result

🔁 Integer Overflow

What happens when you exceed the limit?

4 Bytes / 32 Bits
MIN_VALUE
-2,147,483,648
-231
0
MAX_VALUE
2,147,483,647
231 - 1
MAX + 1 = WRAPS TO MIN
Constants.java

🔍 Overflow Quiz (1/3)

What value is printed?

OverflowQuiz.java

🔍 Overflow Quiz (2/3)

What value is printed?

OverflowQuiz.java

🔍 Overflow Quiz (3/3)

What value is printed?

OverflowQuiz.java

🧩 Almost Exact

Some decimal values do not fit perfectly into a computer's finite storage.

🌊 Round-off Error

Why doubles are sometimes 'approximate'

Limited Precision

Some decimal values cannot be stored exactly as double values.

Java stores a very close approximation instead.

The Result

Calculations can contain small precision errors.

A double result is not always exact.

PrecisionTest.java
Console Output
0.30000000000000004

⚡ Compound Operators

Write less, mean the same — each line starts from xp = 10

RegularForm.java
CompoundForm.java

Pattern

For compatible types, x op= y performs the same update as x = x op y.

Example: int x = 1; x += 0.5; compiles and stores 1 because the result is cast back to int.

x += y and x = x + yare not always compile-equivalent when the result type changes.

🔍 Compound Quiz (1/7)

What value is printed?

CompoundQuiz.java

🔍 Compound Quiz (2/7)

What value is printed?

CompoundQuiz.java

🔍 Compound Quiz (3/7)

What value is printed?

CompoundQuiz.java

🔍 Compound Quiz (4/7)

What value is printed?

CompoundQuiz.java

🔍 Compound Quiz (5/7)

What value is printed?

CompoundQuiz.java

🔍 Compound Quiz (6/7)

What value is printed?

CompoundQuiz.java

🔍 Compound Quiz (7/7)

What value is printed?

CompoundQuiz.java

➕ Increment & Decrement

The ultimate one-step shortcut

RegularForm.java
ShortcutForm.java

x++ (Increment)

Shorthand for x = x + 1.

Increases the value by exactly 1.

x-- (Decrement)

Shorthand for x = x - 1.

Decreases the value by exactly 1.

🔍 Increment Quiz (1/4)

What value is printed?

IncrementQuiz.java

🔍 Increment Quiz (2/4)

What value is printed?

IncrementQuiz.java

🔍 Increment Quiz (3/4)

What value is printed?

IncrementQuiz.java

🔍 Increment Quiz (4/4)

What value is printed?

IncrementQuiz.java

💡 Increment in Assignment

Recognize when the update happens inside a larger expression.

IncrementAssignment.java

Using ++ or -- inside a larger expression is not part of the AP CSA exam scope.

🔍 Increment Expression Quiz

Extension: What are the final values of a and b?

IncrementExpression.java

🤔 Direct Swap?

This looks reasonable at first glance. What value disappears?

SwapDemo.java

Think First

After a = b, what value does a hold?

When the next line runs, decide whether the original 7 still exists anywhere.

🔄 Swap Visual

Before we write the fix, look at where each value moves.

🔁 Swap Two Values

A temporary variable keeps one value safe while the other moves.

SwapDemo.java

📝 Homework

Complete it on time and submit it before the deadline.

🧭 Next Class

Check what comes next and arrive ready.

🔖 Summary

Key takeaways from this lesson.

Casting

(int) 3.9 becomes 3 through truncation.

Integer Range

MAX_VALUE + 1 wraps to MIN_VALUE.

Round-off

double values can contain small precision errors.

Shortcuts

Compound operators and ++ / -- update values concisely.

01 / 45