Unit 1.5: Casting and Range of Variables
Casting Operators
The casting operators (int) and (double) are used to explicitly convert between double and int values. This is required when converting from a larger data type to a smaller one, as there is a potential loss of information.
- Casting is a temporary conversion of a value from one type to another. It is required when moving from a "larger" type (like
double) to a "smaller" type (likeint) to acknowledge the potential loss of precision.
Task: Casting a double value to an int.
double myDouble = 9.78;
// Explicit cast is required to convert double to int
int myInt = (int) myDouble;
Truncation
Casting a double to an int truncates the value, meaning the decimal part is simply cut off. It does not round.
- Truncation removes the fractional portion of a number. For example, both
9.1and9.9truncate to9.
Task: Observing truncation when casting.
double value = 9.999;
int truncatedValue = (int) value; // truncatedValue is 9
Widening Conversion
When assigning a value from a "smaller" data type to a "larger" one (like int to double), Java performs an automatic widening conversion. No explicit cast is needed.
- Java automatically converts types when there is no risk of losing data. Since every
intcan be perfectly represented as adouble, no special syntax is required.
Task: Assigning an int to a double variable.
int myInt = 10;
double myDouble = myInt; // myDouble is now 10.0. No cast needed.
Casting in Expressions
Explicit casting is often used in arithmetic expressions to ensure a floating-point result when dividing two integers. This prevents integer division.
Task: Using a cast to prevent integer division.
int total = 10;
int count = 4;
double average = (double) total / count; // average is 2.5
// Without the cast, 10 / 4 would be 2 (integer division)
Rounding a Double
To round a positive double to the nearest integer, add 0.5 before casting to an int. For negative numbers, subtract 0.5.
- By adding
0.5before truncating, you ensure that any fractional part>= 0.5pushes the number to the next integer, effectively rounding it.
Task: Rounding positive and negative doubles to the nearest integer.
double num = 2.7;
int roundedNum = (int) (num + 0.5); // roundedNum is 3
double negNum = -2.7;
int roundedNeg = (int) (negNum - 0.5); // roundedNeg is -3
Integer Constants
The Integer class provides constants for the maximum and minimum values an int can store.
- Because computers have finite memory, every numeric type has a maximum and minimum capacity. Attempting to go beyond these limits results in overflow.
Integer.MAX_VALUE: The largest possibleintvalue (2,147,483,647).Integer.MIN_VALUE: The smallest possibleintvalue (-2,147,483,648).
Finite Memory for Integers
An int is stored in 4 bytes (32 bits) of memory. This finite size is what limits its range.

How the math works:
Just as shown in the diagram for a byte (8 bits), the maximum value is determined by the number of bits available for the value (excluding the sign bit):
- Byte (8 bits): 1 sign bit + 7 value bits. Max = .
- Int (32 bits): 1 sign bit + 31 value bits. Max = .
Integer Overflow
If an arithmetic operation produces a result outside the int range, an integer overflow occurs, and the value will "wrap around".
Task: Demonstrating integer overflow.
int x = Integer.MAX_VALUE;
x = x + 1; // Overflow
System.out.println(x); // Prints Integer.MIN_VALUE
How it works in Binary:
When you add 1 to a sequence of 1s in binary, the value "carries over" to the next column, much like 9 + 1 becomes 10 in decimal.
0111 1111 (MAX_VALUE: 127 in a byte)
+ 0000 0001 (Add 1)
-----------
1000 0000 (The 1s ripple all the way to the sign bit!)
This flips the sign bit from 0 (positive) to 1 (negative).

Two's Complement
Java uses Two's Complement to represent integers. To find the binary representation of a negative number, follow these two steps:
- Flip the bits: Change all
0s to1s and1s to0s. - Add 1.
Example: Converting 1 to -1 (8-bit simplified)
- Start with
1:0000 0001 - Flip bits:
1111 1110 - Add 1:
1111 1111(This is-1)
The Asymmetry (Range)
Because 0 is represented as 0000 0000 (which takes up one of the "positive-looking" slots starting with 0), there is one more negative number than positive.
- Max Positive:
0111 1111(127) - Most Negative:
1000 0000(-128) - There is no
+128in an 8-bit signed integer.
Why is 1000 0000 equal to -128?
You can see this by adding 1 to the maximum positive value (127):
0111 1111 (127)
+ 0000 0001 (1)
-----------
1000 0000 (-128)
In Two's Complement, the leading bit has a negative weight. For an 8-bit number, the leading bit is the -128's place.
1000 0000=1000 0001=1111 1111=

Round-off Error
Computers allot a specified amount of memory to store data based on the data type. However, the root cause of these errors is how computers store numbers: they use binary (base-2).
- Some decimal fractions (like
0.1) cannot be represented exactly in binary. This leads to tiny imprecisions that can accumulate during calculations.
Just as 1/3 cannot be written exactly in decimal (it becomes 0.3333...), numbers like 0.1 and 0.2 cannot be written exactly in binary—they become repeating binary fractions.
- Because the computer has finite memory (64 bits for a
double), it must chop off this repeating pattern at some point, storing an approximation. - When you add two approximations (
0.1 + 0.2), the tiny errors compound, resulting in a value slightly larger than0.3.
To avoid these errors in precise calculations (like money), use int values to count cents rather than double dollars.
Task: Observing round-off error with double values.
double price1 = 0.10;
double price2 = 0.20;
System.out.println(price1 + price2); // Prints 0.30000000000000004
Task: Avoiding round-off error using int for precise calculations.
int price1Cents = 10;
int price2Cents = 20;
int totalCents = price1Cents + price2Cents;
System.out.println(totalCents / 100.0); // Prints 0.3 (exactly)