TOPIC 1.5 · STUDY

Casting and Range of Variables

Casting changes a value's type. (int) drops the fractional part toward zero. (double) widens an int so division can keep a fraction. Every int sits between Integer.MIN_VALUE and Integer.MAX_VALUE; a result outside that range overflows. Repeated double arithmetic can miss an expected decimal because of round-off, which is not the same as int overflow.

1.5.A

Casting between int and double

A cast tells Java to treat a value as a different type. Writing (int) in front of a double keeps only the whole-number part. Writing (double) in front of an int produces a double with the same whole value and a .0 fractional part.

Casting a double to int truncates toward zero. Positive 12.9 becomes 12. Negative -9.75 becomes -9, not -10. The fractional digits are discarded; the result is not rounded to the nearest int.

A double 12.9 is cast to int. An arrow points to the truncated result 12, with a note that the .9 is dropped toward zero.

Casting (int) 12.9 drops the fraction and keeps 12. The same rule sends -9.75 to -9.

double price = 12.9;
int dollars = (int) price;   // 12

double chill = -9.75;
int shown = (int) chill;     // -9

int tickets = 17;
int seats = 4;
double fair = (double) tickets / seats;   // 4.25
double lost = (double) (tickets / seats); // 4.0

Order matters when int values are divided. Casting one operand to double before the division keeps the fractional part: (double) tickets / seats is 4.25. Casting after integer division cannot recover what was already discarded: (double) (tickets / seats) is 4.0 because 17 / 4 was already truncated to 4.

Expression What happens Result
(int) 12.9 Truncate toward zero 12
(int) -9.75 Truncate toward zero -9
(double) 17 / 4 Widen, then divide 4.25
(double) (17 / 4) Integer divide first 4.0

Key points

  • (int) truncates a double toward zero; it does not round.
  • (double) widens an int so later arithmetic can keep a fraction.
  • Cast before dividing when you need a fractional quotient from int operands.
  • Casting after integer division cannot restore the discarded remainder.

1.5.B

Range of int values

Every int value sits in a fixed range. Integer.MIN_VALUE is -2147483648. Integer.MAX_VALUE is 2147483647. Both endpoints are included. Any int expression whose mathematical result falls outside that range overflows: the stored bits wrap, and the printed value is wrong for the intended count.

A horizontal int range from MIN_VALUE to MAX_VALUE. Arrows past each end mark overflow when a result leaves the range.

Results past either end of the int range overflow. The program may keep running with a wrapped value.

int low = Integer.MIN_VALUE;  // -2147483648
int high = Integer.MAX_VALUE;  // 2147483647

int tooLow = Integer.MIN_VALUE - 8;  // outside the range
int tooHigh = Integer.MAX_VALUE + 1; // outside the range

Integer.MIN_VALUE - 8 is outside the allowed range, so that expression overflows. The same idea applies past the high end: Integer.MAX_VALUE + 1 is also outside the range. Overflow is about int limits, not about how doubles store fractions.

Name Value Role
Integer.MIN_VALUE -2147483648 Smallest allowed int, inclusive
Integer.MAX_VALUE 2147483647 Largest allowed int, inclusive
Integer.MIN_VALUE - 8 Outside the range Overflow
Integer.MAX_VALUE + 1 Outside the range Overflow

Key points

  • int runs from Integer.MIN_VALUE through Integer.MAX_VALUE inclusive.
  • Those constants are -2147483648 and 2147483647.
  • A result outside the range overflows and stores a wrapped value.
  • Integer.MIN_VALUE - 8 is one clear case that leaves the range.

1.5.C

Round-off error and choosing a type

A double can represent many fractional values, but not every decimal exactly. Adding 0.1 repeatedly can produce a stored value that is not identical to the decimal you expect. Comparing that result to the expected total with == may be false even when the algorithm looks correct on paper.

double total = 0.0;
total = total + 0.1;
total = total + 0.1;
total = total + 0.1;
// total may not compare equal to 0.3 with ==

That mismatch is round-off error. It comes from how doubles encode fractions. It is not int overflow. Overflow happens when an int result leaves Integer.MIN_VALUE..Integer.MAX_VALUE. Round-off happens when a double cannot store a decimal exactly.

When every value you need can be stored exactly as an int, prefer int. Whole counts of seats, tickets, or score points belong in int. Use double when a fraction is part of the data, and remember that == between doubles can fail after arithmetic.

Situation Prefer Why
Whole counts only int Exact storage for each value
Fractions needed double Can hold a fractional part
0.1 added many times Expect round-off Not the same as int overflow
Result past MAX_VALUE int overflow Outside the int range

Key points

  • Repeated double arithmetic can miss an expected decimal under ==.
  • That failure is round-off, not int overflow.
  • Prefer int when every needed value stores exactly as an int.
  • Use double when a fractional part is required.

Round-off and overflow are different problems. Round-off is about double representation of fractions. Overflow is about an int result leaving Integer.MIN_VALUE through Integer.MAX_VALUE. Do not diagnose one with the language of the other.

Practice this topic