UNIT 1

Unit 1.5: Casting and Range of Variables

AP Computer Science A

Topics Covered:

  • Casting primitive values
  • Integer overflow conditions
  • Accuracy limitations in expressions
  • Memory allocation for data types

Learning Objectives

1.5.A

Develop code to cast primitive values to different primitive types in arithmetic expressions and determine the value that is produced as a result.

1.5.B

Describe conditions when an integer expression evaluates to a value out of range.

1.5.C

Describe conditions that limit accuracy of expressions.

Casting Operators

Essential Knowledge 1.5.A.1

The casting operators (int) and (double) can be used to convert from a double value to an int value (or vice versa).

Double to Int

double d = 7.8; int i = (int) d; // i = 7 (truncated)

Int to Double

int i = 5; double d = (double) i; // d = 5.0
Important: Casting a double to int truncates (cuts off) the decimal portion, it does NOT round!

Truncation vs Rounding

Essential Knowledge 1.5.A.2

Casting a double value to an int value causes the digits to the right of the decimal point to be truncated.

Truncation Examples

double a = 3.9; double b = -2.7; int x = (int) a; // x = 3 (not 4!) int y = (int) b; // y = -2 (not -3!)

Essential Knowledge 1.5.A.4

Values of type double can be rounded to the nearest integer by:

  • (int)(x + 0.5) for non-negative numbers
  • (int)(x - 0.5) for negative numbers
// Rounding examples double pos = 3.7; double neg = -2.3; int rounded_pos = (int)(pos + 0.5); // 4 int rounded_neg = (int)(neg - 0.5); // -2

Automatic Casting (Widening)

Essential Knowledge 1.5.A.3

Some code causes int values to be automatically cast (widened) to double values.

When Automatic Casting Occurs

int a = 5; int b = 2; double result1 = a / b; // result1 = 2.0 (int division first!) double result2 = (double)a / b; // result2 = 2.5 (proper division) double result3 = a * 1.0; // result3 = 5.0 (auto-widening)
Common Mistake: If you want decimal results from integer division, cast at least one operand to double BEFORE the operation!

Wrong Way

int x = 7, y = 3; double bad = (double)(x / y); // bad = 2.0

Right Way

int x = 7, y = 3; double good = (double)x / y; // good = 2.333...

Integer Range and Overflow

Essential Knowledge 1.5.B.1 & 1.5.B.2

Integer values are stored using 4 bytes of memory and must be in the range:

  • Integer.MIN_VALUE to Integer.MAX_VALUE
  • Approximately -2.1 billion to +2.1 billion
System.out.println("MIN: " + Integer.MIN_VALUE); // -2147483648 System.out.println("MAX: " + Integer.MAX_VALUE); // 2147483647

Essential Knowledge 1.5.B.3

If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs. The result is an int value in the allowed range but not necessarily the value expected.

Overflow Example

int max = Integer.MAX_VALUE; // 2147483647 int overflow = max + 1; // -2147483648 (wraps around!)

Double Precision and Rounding Errors

Essential Knowledge 1.5.C.1

Computers allot a specified amount of memory to store data based on the data type. If an expression would evaluate to a double that is more precise than can be stored in the allotted amount of memory, a round-off error occurs.

Precision Limitations

double a = 0.1; double b = 0.2; double sum = a + b; System.out.println(sum); // 0.30000000000000004 (not exactly 0.3!)
Best Practice: To avoid rounding errors that naturally occur, use int values when possible (e.g., work in cents instead of dollars for money calculations).
// Better approach for money calculations int cents1 = 10; // 10 cents ($0.10) int cents2 = 20; // 20 cents ($0.20) int total = cents1 + cents2; // 30 cents exactly

Summary

Casting

  • Use (int) and (double) to convert
  • double → int truncates decimals
  • int → double adds .0
  • Automatic widening occurs in mixed operations

Range & Precision

  • int: ±2.1 billion range
  • Overflow wraps around
  • double has precision limits
  • Use int when exact values matter

Quick Practice

What will this code output?

int x = 7; int y = 3; double result = (double)(x / y); System.out.println(result);

Answer: 2.0 (integer division happens first, then cast to double)

1 / ?