Now that you understand the basics of arithmetic operators, it is vital to talk about prim-itive numeric promotion, as Java may do things that seem unusual to you at first. As we showed in Chapter 1, “Building Blocks,” each primitive numeric type has a bit-length. You don’t need to know the exact size of these types for the exam, but you should know which are bigger than others. For example, you should know that a long takes up more space than an int, which in turn takes up more space than a short, and so on.
You need to memorize certain rules that Java will follow when applying operators to data types:
The last two rules are the ones most people have trouble with and the ones likely to trip you up on the exam. For the third rule, note that unary operators are excluded from this rule. For example, applying ++ to a short value results in a short value.
Let’s tackle some examples for illustrative purposes:
What is the data type of x * y?
int x = 1;
long y = 33;
var z = x * y;
In this case, we follow the first rule. Since one of the values is int and the other is long, and long is larger than int, the int value x is first promoted to a long. The result z is then a long value.
What is the data type of x + y?
double x = 39.21;
float y = 2.1;
var z = x + y;
This is actually a trick question, as the second line does not compile! As you may remember from Chapter 1, floating-point literals are assumed to be double unless post-fixed with an f, as in 2.1f. If the value of y was set properly to 2.1f, then the promo-tion would be similar to the previous example, with both operands being promoted to a double, and the result z would be a double value.
What is the data type of x * y?
short x = 10;
short y = 3;
var z = x * y;
On the last line, we must apply the third rule: that x and y will both be promoted to int before the binary multiplication operation, resulting in an output of type int. If you were to try to assign the value to a short variable z without casting, then the code would not compile. Pay close attention to the fact that the resulting output is not a short, as we’ll come back to this example in the upcoming “Assigning Values” section.
What is the data type of w * x / y?
short w = 14;
float x = 13;
double y = 30;
var z = w * x / y;
In this case, we must apply all of the rules. First, w will automatically be promoted to int solely because it is a short and is being used in an arithmetic binary operation. The promoted w value will then be automatically promoted to a float so that it can be mul-tiplied with x. The result of w * x will then be automatically promoted to a double so that it can be divided by y, resulting in a double value.
When working with arithmetic operators in Java, you should always be aware of the data type of variables, intermediate values, and resulting values. You should apply operator prece-dence and parentheses and work outward, promoting data types along the way. In the next section, we’ll discuss the intricacies of assigning these values to variables of a particular type.