3.6. Casting Variables¶
Run this code to find how Java handles division and what casting can do to the results.
Java assumes that if you are doing division with integers that you want an integer result and it will throw away any fractional part (part after the decimal point). But, if you use a mixture of integers (int) and floating point (double) numbers Java will assume that you want a floating point result. If you have integers and you want a floating point result from some mathematical operation cast one of the integers to a double using (double) as shown above. 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 to the right of the cast to the specified type.
Is the result of 1.0 divided by 3 what you expected? Java limits the number of digits you can save for any double
number to about 1415 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.
Check your understanding

351: True or false: Java rounds up automatically when you do integer division.
 true
 Did you try this out in Dr Java? Does it work that way?
 false
 Java throws away any values after the decimal point if you do integer division. It does not round up automatically.

352: True or false: casting always results in a double type.
 true
 Try casting to int instead of double. What does that do?
 false
 Casting results in the type that you cast to. However, if you can't really cast the value to the specified type then you will get an error.

353: Which of the following returns the correct average when 3 values had been added to an integer total?
 (double) (total / 3);
 This does integer division before casting the result to double so it loses the fractional part.
 total / 3;
 When you divide an integer by an integer you get an integer result and lose the fractional part.
 (double) total / 3;
 This will convert total to a double value and then divide by 3 to return a double result.