Mastering data conversion, navigating the limits of memory, and writing efficient shortcuts.
When a big drink meets a small cup, something has to be left behind.
Why can't we just assign a double to an int?
A double holds more information than an int. Java refuses to silently lose that data.
Use an explicit cast to tell Java that the possible data loss is intentional.
Changing types: Narrowing vs. Widening
Convert a larger type to a smaller type.
double → int requires an explicit cast because data may be lost.
Convert a smaller type to a larger type.
int → double happens automatically because the value remains representable.
Use (int) someDouble to narrow or (double) someInt to widen explicitly.
Compare what Java requires when values move between int and double.
Narrowing: double → int
Requires an explicit cast because the decimal part may be lost.
Widening: int → double
Happens automatically because no integer information is lost.
Casting and arithmetic happen before the final assignment.
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.
Casting cuts the decimal — it never rounds
The decimal portion is dropped, not rounded.
Even 3.9 becomes 3.
Truncation always drops the fractional part.
For negative values, dropping the decimal moves the result toward zero.
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
How to achieve (nearest int) instead of just truncation
Add 0.5 before casting.
Pattern: (int)(x + 0.5)
Subtract 0.5 before casting.
Pattern: (int)(x - 0.5)
Positive Example
Negative Example
What happens when you exceed the limit?
-2,147,483,6482,147,483,647What value is printed?
What value is printed?
What value is printed?
Some decimal values do not fit perfectly into a computer's finite storage.
Why doubles are sometimes 'approximate'
Some decimal values cannot be stored exactly as double values.
Java stores a very close approximation instead.
Calculations can contain small precision errors.
A double result is not always exact.
Write less, mean the same — each line starts from xp = 10
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.
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
What value is printed?
The ultimate one-step shortcut
Shorthand for x = x + 1.
Increases the value by exactly 1.
Shorthand for x = x - 1.
Decreases the value by exactly 1.
What value is printed?
What value is printed?
What value is printed?
What value is printed?
Recognize when the update happens inside a larger expression.
Using ++ or -- inside a larger expression is not part of the AP CSA exam scope.
Extension: What are the final values of a and b?
This looks reasonable at first glance. What value disappears?
After a = b, what value does a hold?
When the next line runs, decide whether the original 7 still exists anywhere.
Before we write the fix, look at where each value moves.
A temporary variable keeps one value safe while the other moves.
Complete it on time and submit it before the deadline.
Check what comes next and arrive ready.
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.