Unit 1.5: Casting and Ranges of Values¶
Scope: CS Awesome 2, Section 1.5
Learning Goals¶
By the end of this lesson, you should be able to:
- Convert between
intanddoubleusing casts. - Distinguish truncation from rounding.
- Predict when Java widens an
intautomatically. - Describe integer overflow and floating-point round-off.
- Use numeric limits deliberately.
Type Casting¶
Type casting converts a value from one type to another.
double to int¶
double measurement = 8.93;
int whole = (int) measurement; // 8
Casting a double to int truncates the fractional part. It does not round.
System.out.println((int) 4.99); // 4
System.out.println((int) -4.99); // -4
Truncation moves toward zero.
int to double¶
int total = 17;
double preciseTotal = (double) total; // 17.0
Java can usually widen an int to double automatically.
double preciseTotal = total;
In an arithmetic expression, one double operand causes numeric int operands to be widened so the result is double.
System.out.println(5 + 2.0); // 7.0
Casting Before or After Division¶
Cast placement can change the result.
int total = 10;
double a = (double) (total / 4); // 2.0: integer division happened first
double b = (double) total / 4; // 2.5: total widened before division
To preserve a fractional quotient, make at least one operand a double before division.
Rounding¶
For a nonnegative double x, a common nearest-integer pattern is:
int rounded = (int) (x + 0.5);
For a negative value:
int rounded = (int) (x - 0.5);
double positive = 6.7;
double negative = -6.7;
System.out.println((int) (positive + 0.5)); // 7
System.out.println((int) (negative - 0.5)); // -7
Parentheses matter because the addition or subtraction must happen before the cast.
Integer Range and Overflow¶
Java int values use a finite amount of memory. Their inclusive limits are available as constants:
System.out.println(Integer.MIN_VALUE); // -2147483648
System.out.println(Integer.MAX_VALUE); // 2147483647
If integer arithmetic exceeds this range, overflow occurs. Java wraps to another value in the valid range instead of automatically reporting an error.
int largest = Integer.MAX_VALUE;
System.out.println(largest + 1); // -2147483648
The expression compiles and runs, but the result may violate the program's assumptions.
Floating-Point Round-Off¶
Many decimal values cannot be represented exactly in binary floating-point.
System.out.println(0.1 + 0.2); // may display 0.30000000000000004
This is round-off error, not a mistake in addition. For quantities such as money, an integer number of the smallest unit can avoid some floating-point issues.
int priceInWon = 1250;
Practice Missions¶
Mission 1: Cast Placement¶
Predict each value and explain where integer division occurs.
(double) (11 / 4)
(double) 11 / 4
11 / (double) 4
(int) (11.9 / 4)
Mission 2: Sensor Average¶
Three sensor readings are stored as int values. Write an expression that calculates their average as a double without losing the fractional part. Use a cast rather than a 3.0 literal.
Mission 3: Round Both Directions¶
Write code that rounds 12.6 and -12.6 to their nearest integers using casts and arithmetic. Explain why one formula is not correct for both signs.
Mission 4: Overflow Investigation¶
Predict and then run this code. Explain why no compiler error is required even though the mathematical result is larger than an int can store.
int value = Integer.MAX_VALUE;
value = value + 2;
System.out.println(value);
Mission 5: Representation Choice¶
A transit card stores balances. Compare storing the balance as dollars in a double with storing it as cents in an int. State one advantage and one limitation of each design.
Key Summary¶
| Concept | Core idea |
|---|---|
(int) |
Converts to int by truncating toward zero. |
(double) |
Converts or widens a numeric value to double. |
| Cast placement | Determines whether conversion occurs before an operation. |
| Rounding pattern | Adjust by 0.5, then cast, with sign considered. |
| Integer overflow | Arithmetic leaves the valid int range and wraps. |
| Round-off error | A decimal cannot be represented with perfect precision. |
Source scope: CS Awesome 2, Unit 1.5