1.5 - Casting and Range of Variables
You can convert between
double and int using the casting operators (int) and (double).double d = 9.7; int i = (int) d; // i = 9, the decimal part just gets chopped off, not rounded
Casting a
double to an int truncates it, meaning it just cuts off everything after the decimal point rather than rounding.int a = (int) 3.99; // 3, not 4 int b = (int) -2.7; // -2, truncating always moves toward 0
Casting the other way,
(double) turns an int into a double. This is how you get a real average from int values:int total = 17; int count = 4; double avg1 = total / count; // 4.0, int division happens first double avg2 = (double) total / count; // 4.25 double avg3 = (double) (total / count); // 4.0, the cast happens after int division
A cast only applies to the value right after it, and it happens before multiplication, division, and remainder. So
(double) total / count turns total into 17.0 first, and 17.0 / 4 is real division. Putting total / count in parentheses makes the int division happen first, so you’re just casting the already chopped 4 into 4.0.Sometimes Java automatically widens an
int to a double for you, no cast needed.int wholeNumber = 5; double widened = wholeNumber; // automatically becomes 5.0
Widening also happens in the middle of an expression, or when you pass an
int to a method that expects a double:double x = 5 + 2.0; // 7.0, the 5 is widened to 5.0 double root = Math.sqrt(16); // 4.0, 16 is widened to 16.0 for the double parameter
Java never automatically narrows a
double into an int, since that could lose information. int n = 7.0; won’t compile. You need the cast: int n = (int) 7.0;To actually round a double to the nearest integer, you can use:
double x = 4.6; int rounded = (int)(x + 0.5); // 5
Why it works: adding 0.5 pushes any number whose decimal part is .5 or more past the next whole number, and then truncation chops off the decimal. For negative numbers you subtract 0.5 instead, since truncation moves toward 0.
double p = 4.4; int r1 = (int) (p + 0.5); // (int) 4.9 = 4 double n = -4.6; int r2 = (int) (n - 0.5); // (int) -5.1 = -5
Integer Range
Integer.MAX_VALUE is the largest possible int value, and Integer.MIN_VALUE is the smallest possible one. That’s because an int is stored using a fixed amount of memory (4 bytes), so it has to fall somewhere between those two limits.Their actual values are 2147483647 for
Integer.MAX_VALUE (that’s ) and -2147483648 for Integer.MIN_VALUE (that’s ). An int can be any value from MIN_VALUE to MAX_VALUE, including both. You don’t need to memorize the numbers, just use the constants.If a calculation would push the result outside that range, you get integer overflow. Java doesn’t throw an error for this, it just wraps around to some value inside the allowed range, which is probably not what you expected.
int max = Integer.MAX_VALUE; int overflowed = max + 1; // wraps around to Integer.MIN_VALUE
System.out.println(Integer.MAX_VALUE + 1); // -2147483648 System.out.println(Integer.MIN_VALUE - 1); // 2147483647 int big = 50000 * 50000; // overflow: big is -1794967296, not 2500000000
Overflow doesn’t cause an error or an exception, so the program keeps running with the wrong number. Picture the
int values on a loop: going one past the largest value lands on the smallest one, and going one below the smallest lands on the largest.Round-off Error
Computers only allot so much memory to store a
double. If a calculation would need more precision than that memory can hold, you get a round-off error, meaning the result gets rounded to the closest value that can actually be represented. If you want to avoid this entirely, use int values instead when you can.System.out.println(0.1 + 0.2); // 0.30000000000000004, not 0.3 System.out.println(0.1 + 0.2 == 0.3); // false
This happens because a
double stores numbers in binary, and many decimals (like 0.1) can’t be written exactly in binary, the same way 1/3 can’t be written exactly as a decimal. That’s why programs that track money often store whole cents in an int (like 499 for $4.99) instead of dollars in a double.