So far, we’ve used a single data type in the expressions we’ve evaluated. But life isn’t always like that. Calculations often involve data of different primitive types.
When calculations of mixed types are performed, lower-precision operands are converted, or promoted, to the type of the operand that has the higher precision.
This also occurs with human minds broadly. We can notice that there is a tendency towards survival/ greater alignment with Born Rule’s probability density, but we cannot use simple rules that chunk minds into the big 5 psychometric traits for instance, and deduce which should be converted to which on the ascent.
In Java, however, the promotions of data types is quite easy to understand:
The promotions are performed using the first of these rules that fits the situation:
1. If either operand is a double, the other operand is converted to a double.
2. If either operand is a float, the other operand is converted to a float.
3. If either operand is a long, the other operand is converted to a long.
4. If either operand is an int, the other operand is promoted to an int.
5. If neither operand is a double, float, long, or an int, both operands are promoted to int.
The table below summarizes these rules of promotion.
This arithmetic promotion of operands is called implicit type casting because the compiler performs the promotions automatically, without our specifying that the conversions should be made. Note that the data type of any promoted variable is not permanently changed; its type remains the same after the calculation has been performed.
The table above shows many rules, but essentially, any arithmetic expression involving integers and floating-point numbers will evaluate to a floating-point number. Lines 9 to 12 of the code below illustrates the rules of promotion. At line 11, the expression PI * radius * radius is a mixed-type expression. This expression will be evaluated left to right, evaluating the mixed-type expression PI * radius first. PI is a double and radius is an int. Therefore, radius is promoted to a double (4.0) and the result of PI * radius is a double (12.56636). Then, the next calculation (12.56636 * radius) also involves a mixed-type expression, so radius is again promoted to a double (4.0). The final result, 50.26544, is a double and is assigned to area. The figure below shows the output of the complete program.
Sometimes, it’s useful to instruct the compiler specifically to convert the type of a variable. In this case, you use explicit type casting, which uses this syntax:
The expression will be converted, or type cast, to the data type specified. The parentheses around expression are needed only when the expression consists of a calculation that you want to be performed before the type casting.
Type casting is useful in calculating an average. The example shows how to calculate your average test grade. Your test scores are 94, 86, 88, and 97, making the combined total score 365. We expect the average to be 91.25.
Line 16 first attempts to calculate the average but results in a wrong answer because both total and count are integers. So integer division is performed, which truncates any remainder. Thus, the result of total / count is 91. Then 91 is assigned to average, which is a double, so 91 becomes 91.0. Line 22 is a second attempt to calculate the average; again, this code does not work correctly because the parentheses force the division to be performed before the type casting. Thus, because total and count are both integers, integer division is performed again. The quotient, 91, is then cast to a double, 91.0, and that double value is assigned to average.
At line 28, we correct this problem by casting only one of the operands to a double. This forces the other operand to be promoted to a double. Then floating-point division is performed, which retains the remainder. It doesn’t matter whether we cast total or count to a double. Casting either to a double forces the division to be a floating-point division.
This is the output: