Assigning Values – Working with Binary Arithmetic Operators – 1Z0-829 Study Guide

Assigning Values

Compilation errors from assignment operators are often overlooked on the exam, in part because of how subtle these errors can be. To be successful with the assignment operators, you should be fluent in understanding how the compiler handles numeric promotion and when casting is required. Being able to spot these issues is critical to passing the exam, as assignment operators appear in nearly every question with a code snippet.

Assignment Operator

An assignment operator is a binary operator that modifies, or assigns, the variable on the left side of the operator with the result of the value on the right side of the equation. Unlike most other Java operators, the assignment operator is evaluated from right to left.

The simplest assignment operator is the = assignment, which you have seen already:

int herd = 1;

This statement assigns the herd variable the value of 1.

Java will automatically promote from smaller to larger data types, as you saw in the previous section on arithmetic operators, but it will throw a compiler exception if it detects that you are trying to convert from larger to smaller data types without casting. Table 2.5 lists the first assignment operator that you need to know for the exam. We present additional assignment operators later in this section.

TABLE 2.5Simple assignment operator
OperatorExampleDescription
Assignmentint a = 50;Assigns the value on the right to the variable on the left

Casting Values

Seems easy so far, right? Well, we can’t really talk about the assignment operator in detail until we’ve covered casting. Casting is a unary operation where one data type is explicitly interpreted as another data type. Casting is optional and unnecessary when converting to a larger or widening data type, but it is required when converting to a smaller or narrowing data type. Without casting, the compiler will generate an error when trying to put a larger data type inside a smaller one.

Casting is performed by placing the data type, enclosed in parentheses, to the left of the value you want to cast. Here are some examples of casting:

int fur = (int)5;

int hair = (short) 2;

String type = (String) “Bird”;

short tail = (short)(4 + 10);

long feathers = 10(long); // DOES NOT COMPILE

Spaces between the cast and the value are optional. As shown in the second-­to-­last example, it is common for the right side to also be in parentheses. Since casting is a unary operation, it would only be applied to the 4 if we didn’t enclose 4 + 10 in parentheses. The last example does not compile because the type is on the wrong side of the value.

On the one hand, it is convenient that the compiler automatically casts smaller data types to larger ones. On the other hand, it makes for great exam questions when they do the opposite to see whether you are paying attention. See if you can figure out why none of the following lines of code compile:

float egg = 2.0 / 9; // DOES NOT COMPILE
int tadpole = (int)5* 2L;//DOESNOT COMPILE
short frog = 3 -­2.0;//DOESNOT COMPILE

All of these examples involve putting a larger value into a smaller data type. Don’t worry if you don’t follow this quite yet; we cover more examples like this shortly.

In this chapter, casting is primarily concerned with converting numeric data types into other data types. As you will see in later chapters, casting can also be applied to objects and references. In those cases, though, no conversion is performed. Put simply, casting a numeric value may change the data type, while casting an object only changes the reference to the object, not the object itself.