Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
1.6. Casting and Ranges of Variables¶
In Java, type casting is used to convert variables from one type to another. By casting we don’t mean something to do with fishing, but it is a similar idea to casting a pot in clay. In Java when you cast you are changing the “shape” (or type) of the variable.
The casting operators (int) and (double) are used right next to a number or variable to create a temporary value converted to a different data type. For example,
(double) 1/3 will give a double result instead of an int one. Run this code to find how Java handles division and what casting can do to the results. Notice what happens when you divide an int by an int or an int by a double or an int casted to a double divided by an int.
What happens when you divide an int by an int or with a double operand or with the type cast (double) on one of the operands?
Java assumes that if you are doing division with integers that you want an integer result and it will truncate and throw away the part after the decimal point. But, if you use a mixture of integers (int) and decimal (double) numbers Java will assume that you want a double result. If there is at least one double in the operation, Java will widen the type of the other operand to double too and return the result in a double. If you have integers and you want a double result from some mathematical operation cast one of the integers to a double using (double) as shown above.
Values of type double can be rounded to the nearest integer by adding or subtracting .5 and casting with (int) using formulas like the following.
int nearestInt = (int)(number + 0.5);
int nearestNegInt = (int)(negNumber – 0.5);
For example, if you divide 5/3 using integer division, Java will truncate 1.67 to 1 to give an int result. However, we usually round up any answer .5 and above. Using the formula above, if we add 1.67 + 0.50, we get 2.17 and then casting it to an int throws away what’s after the decimal point, just leaving 2.
Run the code below to see how the formula of adding or subtracting .5 and then casting with (int) rounds a positive or negative double number to the closest int.
What happens to repeating decimal numbers like 3.333333…? Java limits the number of digits you can save for any
double number to about 14-15 digits. You should be aware that the accuracy of any calculation on a computer is limited by the fact that computers can only hold a limited number of digits.
For example, int values are stored in 4 bytes of memory. There is an Integer.MAX_VALUE defined as 2147483647 and an Integer.MIN_VALUE defined as -2147483648. If you try to store any number larger or smaller than these numbers in an int variable, it will result in an error called integer overflow. Try it below.
Try the code below to see two integer overflow errors for a positive and negative number. An int cannot hold that many digits! Fix the integer overflow error by deleting the last 0 in the numbers.
You can format long decimal numbers to just show 2 digits after the decimal point with the following code:
Run the code below to see how a decimal number can be formatted to show 2 digits after the decimal point.
1.6.1. Programming Challenge : Average 3 Numbers¶
In the code below, declare and initialize 3 int variables that represent grades, and then sum and average them. Use casting to report the result as a double. For example, if the grades are 90, 100, and 94, the sum of the three numbers is 90 + 100 + 94 = 284, and the average is the sum 284 divided by 3 which casted to a double is 94.666667. You should use variables instead of the numeric literals in your formulas. Follow the pseudocode below.
Sum and average 3 grades. Your grade variables must have type int, not double. Use type casting to compute the average as a double. For an extra challenge, use a Scanner to read the grades from standard input.
Type casting is used to convert variables from one type to another.
The casting operators (int) and (double) can be used to create a temporary value converted to a different data type.
Casting a double value to an int causes the digits to the right of the decimal point to be truncated (cut off and thrown away).
Some programming code causes int values to be automatically cast (widened) to double values.
Values of type double can be rounded to the nearest integer by (int)(x + 0.5) or (int)(x – 0.5) for negative numbers.
Integer values in Java are represented by values of type int, which are stored using a finite amount (4 bytes) of memory. Therefore, an int value must be in the range from Integer.MIN_VALUE to Integer.MAX_VALUE inclusive.
If an expression would evaluate to an int value outside of the allowed range, an integer overflow occurs. This could result in an incorrect value within the allowed range.