Casting Values vs. Variables – Working with Binary Arithmetic Operators – 1Z0-829 Study Guide

Casting Values vs. Variables

Revisiting our third numeric promotional rule, the compiler doesn’t require casting when working with literal values that fit into the data type. Consider these examples:

bytehat = 1;
bytegloves =7*10;
shortscarf=5;
shortboots=2+1;

All of these statements compile without issue. On the other hand, neither of these state-ments compiles:

short boots = 2 + hat; // DOES NOT COMPILE byte gloves = 7 * 100; // DOES NOT COMPILE

The first statement does not compile because hat is a variable, not a value, and both operands are automatically promoted to int. When working with values, the compiler had enough information to determine the writer’s intent. When working with variables, though, there is ambiguity about how to proceed, so the compiler reports an error. The sec-ond expression does not compile because 700 triggers an overflow for byte, which has a maximum value of 127.

Compound Assignment Operators

Besides the simple assignment operator (=), Java supports numerous compound assignment operators. For the exam, you should be familiar with the compound operators in Table 2.6.

TABLE 2.6Compound assignment operators
OperatorExampleDescription
Additiona += 5Adds the value on the right to the variable on the left and
assignment assigns the sum to the variable
Subtractionb -­= 0.2Subtracts the value on the right from the variable on the left
assignment and assigns the difference to the variable
Multiplicationc *= 100Multiplies the value on the right with the variable on the left
assignment and assigns the product to the variable
Divisiond /= 4Divides the variable on the left by the value on the right and
assignment assigns the quotient to the variable

Compound operators are really just glorified forms of the simple assignment operator, with a built-­in arithmetic or logical operation that applies the left and right sides of the statement and stores the resulting value in the variable on the left side of the statement. For example, the following two statements after the declaration of camel and giraffe are equivalent when run independently:

int camel = 2, giraffe = 3;

camel = camel * giraffe;

camel *= giraffe;

// Simple assignment operator

// Compound assignment operator

The left side of the compound operator can be applied only to a variable that is already defined and cannot be used to declare a new variable. In this example, if camel were not already defined, the expression camel *= giraffe would not compile.

Compound operators are useful for more than just shorthand—­they can also save you from having to explicitly cast a value. For example, consider the following. Can you figure out why the last line does not compile?

long goat=10;
int sheep=5;
sheep = sheep * goat;// DOES NOT COMPILE

From the previous section, you should be able to spot the problem in the last line. We are trying to assign a long value to an int variable. This last line could be fixed with an explicit cast to (int), but there’s a better way using the compound assignment operator:

long goat = 10;

int sheep = 5;

sheep *= goat;

The compound operator will first cast sheep to a long, apply the multiplication of two long values, and then cast the result to an int. Unlike the previous example, in which the compiler reported an error, the compiler will automatically cast the resulting value to the data type of the value on the left side of the compound operator.