In Introduction to Arithmetic Expressions tutorial, we learned that there are two different types of division.
This means that division functions in two different ways:
- If both operands are of type
int, the result truncates (removes) any remainder. This is considered integer division. For example:
7 / 2 results in 3, the decimal portion is not included.
4 / 2 results in 2, there is no remainder in this case.
- If one or both operands are of type
double, the result includes the remainder. For example:
7.0 / 2 results in 3.5. Similarly, 7 / 2.0 and 7.0 / 2.0 will also result in 3.5.
4.0 / 2 results in 2.0. Similarly, 4 / 2.0 and 4.0 / 2.0 will also result in 2.0.
There are often times when you have two int values or variables and want to compute division and have the result include the remainder. There are also times when one of the values is a double and we need the whole part of the division.
You can convert the type of a value through casting. Casting Conversions converts the type of an expression to the type specified. It is done by placing the desired type in parenthesis prior to the expression. In the case of wanting one or both of the operands to be a double, we can cast to a double as follows:
int value1 = 5;
int value2 = 2;
double result = (double)value1 / value2;
This will temporarily convert the type of value1 to a double. When the division is performed, result will now be assigned 2.5, instead of 2. You could also typecast value2 or both value1 and value2 to achieve this same result.
Your Turn
Let's try this in the Java Playground.
- Run the code to determine what is stored in
result.
- Modify the code to typecast
value2 to a double instead of value1.
- Run the code. What do you notice about this result?
- Modify the code to typecast both
value1 and value2 to a double.
- Run the code. What do you notice about this result?
Your Turn: Computing An Average
Consider you want to compute your average for a class. You have two int values:
- the sum of all the grades and
- the number of grades that you have.
When we compute the average of the the two int values, it will truncate the result, essentially always rounding down.
Instead, you might want to have the real-number average.
Let's try this in the Java Playground.
- Cast
sumGrades to be a double by putting (double) in front of sumGrades when computing average.
- Run the code. What do you notice about this result?
- Move the cast from
sumGrades to gradeCount.
- Run the code. What do you notice about this result?
- Cast both
sumGrades and gradeCount.
- Run the code. What do you notice about this result?
Data Conversions
When we typecast from an int to a double, this is considered a widening conversion since the int value fits into the double type without any loss of data.
When we typecast from a double to an int, this is considered a narrowing conversion since the double value might need to be truncated to fit into the int type thereby losing some data.
Your Turn
Let's try this in the Java Playground.
- Cast
average to an int.
- Run the code.
- How does this change the value stored in
truncatedAverage?
Next Learn Tutorial
Learn: Introduction to Console Input and Output