This book is now obsolete Please use CSAwesome instead.
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 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.
Check your understanding
- Did you try this out in Dr Java? Does it work that way?
- Java throws away any values after the decimal point if you do integer division. It does not round up automatically.
3-5-1: True or false: Java rounds up automatically when you do integer division.
- Try casting to int instead of double. What does that do?
- 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.
3-5-2: True or false: casting always results in a double type.
- (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.
3-5-3: Which of the following returns the correct average when 3 values had been added to an integer total?