Casting & Range

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

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

Put the target type in parentheses: (int) someDouble

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.

Quiz: Casting

What is the value of result?

Quiz.java
java

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

Quiz: Overflow

What does this print?

Quiz.java
java

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. Never use == to compare doubles!

PrecisionTest.java
java
Console Output
0.30000000000000004

Quiz: Precision

Predict the output

Quiz.java
java

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.

Quiz: Compound

What is the result?

Quiz.java
java

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.

Quiz: Increment

What is the final value?

Quiz.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

After-Class
Assignment

The Price Tag

  • 1. Store the price as a double.
  • 2. Cast it to an int and print values.
  • 3. Use *= to add a discount.
  • 4. Print the final result.

Score Counter

  • 1. Start with int score = 0.
  • 2. Add 100 points using +=.
  • 3. Double the score using *=.
  • 4. Deduct a penalty using -=.
  • 5. Print the final result.