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

Java is strict

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

The solution: Cast

We need to explicitly cast — tell Java: "I know data may be lost, do it anyway."

Casting

Changing types: Narrowing vs. Widening

Narrowing

From larger to smaller type. Data loss possible — requires explicit cast.
double → int

Widening

From smaller to larger type. No data loss — occurs automatically.
int → double
double
99.9
needs (int)
int
99
int
5
automatic
double
5.0

Syntax

(int) someDouble — converts a double to int

(double) someInt — converts an int to double

Truncation Rules

Casting cuts the decimal — it never rounds

CastingDemo.java
java

99.9 → 99

Decimal portion is dropped, not rounded.

3.9 → 3

Even 3.9 becomes 3. Truncation always drops the fraction.

-2.9 → -2

For negatives: "drop the decimal" results in -2.

Casting Quiz

Truncation & widening

CastingQA.java
java
Question 1 of 10

Rounding Strategy

How to achieve (nearest int) instead of just truncation

Positive Numbers

Add 0.5 before casting.
(int)(x + 0.5)

Negative Numbers

Subtract 0.5 before casting.
(int)(x - 0.5)

The Logic

8.68.6 + 0.59.1
Offset
(int) 9.19
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
java

Overflow Quiz

What happens when numbers exceed their limits?

OverflowQA.java
java
Question 1 of 3

Round-off Error

Why doubles are sometimes 'approximate'

Limited Precision

Some decimal values cannot be stored exactly as doubles. Java keeps a very close approximation instead.

The Result

This leads to tiny precision errors during calculation — doubles are not always exact.

PrecisionTest.java
java
Console Output
0.30000000000000004

Compound Operators

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

CompoundOps.java
java

Pattern

x op= y is always equivalent to x = x op y.

Compound Quiz

Decode the shortcut operators

CompoundQA.java
java
Question 1 of 7

Increment & Decrement

The ultimate one-step shortcut

IncrDemo.java
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

++ and -- in action

IncrementQA.java
java
Question 1 of 4

💡 Increment in Assignment

This pattern is useful to recognize, but it is not part of the AP CSA exam focus.

IncrementAssignment.java
java

Increment Expression

Try tracing this one step by step before you trust your intuition.

IncrementExpression.java
java

Direct Swap?

This looks reasonable at first glance. What value disappears?

SwapDemo.java
java

Think First

After a = b, what is the value of a? When the next line runs, does the original 7 still exist anywhere?

Swap Visual

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

Visual explanation of swapping two variables

Swap Two Values

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

SwapDemo.java
java

Summary

The four things you must remember

Casting is explicit

(int) 3.9 → 3 (truncation, not rounding)

32-bit Integer limits

Integer.MAX_VALUE + 1 wraps to MIN_VALUE (Overflow)

Doubles aren't exact

0.1 + 0.2 can equal 0.30000000000000004 (Round-off Error)

Shortcut Operators

x += 5 (compound) and x++ (increment) save time

01 / 20