Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 143

Numeric Promotion Rules

Оглавление

1 If two values have different data types, Java will automatically promote one of the values to the larger of the two data types.

2 If one of the values is integral and the other is floating-point, Java will automatically promote the integral value to the floating-point value's data type.

3 Smaller data types, namely, byte, short, and char, are first promoted to int any time they're used with a Java binary arithmetic operator with a variable (as opposed to a value), even if neither of the operands is int.

4 After all promotion has occurred and the operands have the same data type, the resulting value will have the same data type as its promoted operands.

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 postfixed with an f, as in 2.1f. If the value of y was set properly to 2.1f, then the promotion 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 multiplied 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 precedence 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.

OCP Oracle Certified Professional Java SE 17 Developer Study Guide

Подняться наверх