Casting and Range of Variables
- Introduction
- Casting in Java
- Range and Overflow in Java Integers
- Python Helper
- Truncation vs. Rounding
- Promotion Rules
- FRQ-style tasks
- MCQs
Introduction
int a = 5, b = 2;
System.out.println(a / b);
System.out.println((double)a / b);
System.out.println((int)3.9);
System.out.println((int)(3.9 + 0.5));
2
2.5
3
4
Casting in Java
public class CastDemo {
public static void main(String[] args) {
int a = 7, b = 2;
System.out.println(a / b); // 3 (int division)
System.out.println((double)a / b); // 3.5 (promoted to double before divide)
double d = 5.9;
int t = (int) d; // 5 (truncate toward 0)
System.out.println(t);
// Rounding to nearest int (per CED):
int rPos = (int)(d + 0.5); // 6 (non-negative rounding)
double n = -5.9;
int rNeg = (int)(n - 0.5); // -6 (negative rounding)
System.out.println(rPos + " " + rNeg);
}
}
CastDemo.main(null);
3
3.5
5
6 -6
Range and Overflow in Java Integers
int max = Integer.MAX_VALUE; // 2147483647
int min = Integer.MIN_VALUE; // -2147483648
System.out.println(max + 1); // overflow → -2147483648
System.out.println(min - 1); // overflow → 2147483647
-2147483648
2147483647
Python Helper
# Utilities to simulate Java 32-bit signed int in Python
INT_MIN = -2**31
INT_MAX = 2**31 - 1
MASK32 = 0xFFFFFFFF
def to_int32(x: int) -> int:
x &= MASK32
# convert to signed
return x if x <= INT_MAX else x - (1 << 32)
def add_int32(a: int, b: int) -> int:
return to_int32(a + b)
def sub_int32(a: int, b: int) -> int:
return to_int32(a - b)
def mul_int32(a: int, b: int) -> int:
return to_int32(a * b)
def div_int32(a: int, b: int) -> int:
# Java int division truncates toward zero
if b == 0:
raise ZeroDivisionError("division by zero")
q = int(a / b) # Python truncates toward zero for int()
return to_int32(q)
def mod_int32(a: int, b: int) -> int:
# Java remainder has same sign as dividend (a)
if b == 0:
raise ZeroDivisionError("mod by zero")
q = div_int32(a, b)
r = to_int32(a - q * b)
return r
# Demo
print("INT_MIN, INT_MAX:", INT_MIN, INT_MAX)
print("Overflow examples:")
print("MAX+1 =", add_int32(INT_MAX, 1))
print("MIN-1 =", sub_int32(INT_MIN, 1))
print("(-7)/2 =", div_int32(-7, 2), " (-7)%2 =", mod_int32(-7, 2))
print("(7)/-2 =", div_int32(7, -2), " (7)%-2 =", mod_int32(7, -2))
INT_MIN, INT_MAX: -2147483648 2147483647
Overflow examples:
MAX+1 = -2147483648
MIN-1 = 2147483647
(-7)/2 = -3 (-7)%2 = -1
(7)/-2 = -3 (7)%-2 = 1
Truncation vs. Rounding
def java_truncate(x: float) -> int:
# Casting double->int truncates toward 0
return int(x) # Python int() also truncates toward 0
def ced_round(x: float) -> int:
if x >= 0:
return int(x + 0.5)
else:
return int(x - 0.5)
tests = [5.1, 5.5, 5.9, -5.1, -5.5, -5.9, 0.49, -0.49]
for v in tests:
print(f"x={v:5} truncate={java_truncate(v):3} ced_round={ced_round(v):3}")
x= 5.1 truncate= 5 ced_round= 5
x= 5.5 truncate= 5 ced_round= 6
x= 5.9 truncate= 5 ced_round= 6
x= -5.1 truncate= -5 ced_round= -5
x= -5.5 truncate= -5 ced_round= -6
x= -5.9 truncate= -5 ced_round= -6
x= 0.49 truncate= 0 ced_round= 0
x=-0.49 truncate= 0 ced_round= 0
Promotion Rules
int a = 3, b = 2;
double x = a / b; // 1.0 (int division then widened)
double y = (double)a / b; // 1.5 (promoted before divide)
System.out.println("x = " + x);
System.out.println("y = " + y);
x = 1.0
y = 1.5
Practice: predict the output (Java)
Q1
My answers: 2, 2, 2.0, 2.5
int a = 10, b = 4;
System.out.println(a / b);
System.out.println(a % b);
System.out.println((double)(a / b));
System.out.println((double)a / b);
2
2
2.0
2.5
Q2
My Answers: -2, -3, 3
double d = -2.6;
System.out.println((int)d);
System.out.println((int)(d - 0.5));
System.out.println((int)(-d + 0.5));
-2
-3
3
Q3
My Answers: 2147483647, -2147483647
int x = Integer.MAX_VALUE;
int y = x + 2;
System.out.println(x);
System.out.println(y);
2147483647
-2147483647
FRQ-style tasks
FRQ 1. Average with correct casting Write a method avgInt that takes two int values and returns their average as a double, preserving the .5 if present.
public static double avgInt(int a, int b) {
return (a + b) / 2.0;
}
FRQ 2. Percentage Given int correct and int total, compute the percentage as a double from 0.0 to 100.0 without losing fractional precision.
public static double gradePoint(int earnedPoints, int maxPoints) {
if (maxPoints == 0) return 0.0;
return 4.0 * earnedPoints / (double) maxPoints;
}
FRQ 3. Safe remainder Implement safeMod(int a, int b) that returns a % b, but if b == 0, it should return 0 instead of throwing.
public static int safeDivide(int numerator, int denominator) {
if (denominator == 0) return 0;
return numerator / denominator;
}
MCQs
double x = 7.9;
int y = (int) x;
System.out.println(y);
7
int a = 10;
double b = 5.5;
int c = (int)(a + b);
int c = a + (int)b;
int num = 9 / 2;
System.out.println(num);
4
double d = 15.7;
int i = (int)(d + 0.5);
System.out.println(i);
16