Lesson 7 | Assignment operators |
Objective | Describe important points about assignment operators. |
Java Assignment Operators
An assignment operator is a binary operator that modifies, or assigns, the variable on the left-hand side of the operator, with the result of the value on the right-hand side of the equation. The simplest assignment operator is the = assignment, which you have seen already:
int x = 1;
This statement assigns x the value of 1. Java will automatically promote from smaller to larger data types, as we saw in the previous
section on arithmetic operators, but it will throw a compiler exception if it detects you are trying to convert from larger to smaller data types.
Let us return to some examples similar to what you saw in Chapter 1 in order to show how casting can resolve these issues:
int x = 1.0; // DOES NOT COMPILE
short y = 1921222; // DOES NOT COMPILE
int z = 9f; // DOES NOT COMPILE
long t = 192301398193810323; // DOES NOT COMPILE
The first statement does not compile because you are trying to assign a double 1.0 to an integer value. Even though the value is a mathematic integer,
by adding .0, you are instructing the compiler to treat it as a double. The second statement does not compile because the literal value 1921222 is outside the range of short and the compiler detects this. The third statement does not compile because of the f added to the end of the number that instructs the compiler to treat the number as floating-point value.
Finally, the last statement does not compile because Java interprets the literal as an int and notices that the value is larger than int allows. The literal would need a postfix L to be considered a long.
Casting and assignment
Java provides the simple assignment operator = and operators that combine binary operations with the assignment operator.
Examples of the latter are += and *=.
The assignment operators that perform binary arithmetic cast their result to the type of the left operand.
For example, if ch is of type char,
ch += 8
,
assigns (char) (ch+8) to ch.
However, the simple assignment = operator does not perform an implicit cast when used with
primitive numeric types.
[1] When the simple assignment operator is used with object references, a few conditions must be met to avoid compilation errors:
- If the type of the left operand is a class C, then the type of the right operand must be a subclass of C or the null value.
- If the type of the left operand is an interface I, then the type of the right operand must be a subinterface of I, a class that implements I, or the null value.
If the above conditions do not hold, you should use an explicit cast to avoid compilation errors.
The value returned by an assignment
The assignment operator, like other Java operators, returns a value. This value is the value assigned to the left operand.
However, unlike other operators, the assignment operator is
right associative. This means that operations of the form a=b=c are evaluated as a=(b=c) instead of the
left associative (a=b)=c.
The
assignment program provides an example of using associativity and the
value returned by the assignment operator.
- Right associative: Operations that are right associative are performed in a right to left manner. That is, x op y op z is evaluated as x op (y op z).
- Left associative: Operations that are left associative are performed in a left to right manner. That is, x op y op z is evaluated as (x op y) op z.
[1]Primitive types: Primitive types are the most basic data types available within the Java language.
There are 8: boolean, byte, char, short, int, long, float and double. These types serve as the building blocks of data manipulation in Java.