- Home
- Objectives

- XyzWs Study Guides
- Study Guides
- Study Notes
- Resources

- Mock Exams
SCJP Study Guide:
Fundamentals
Printer-friendly version |
Mail this to a friend
Assignment Operators
Assignment operators set the value of a variable or expression to some new value. Assignment operators store a value in the object designated by the left operand. There are two kinds of assignment operations: simple assignment, in which the value of the second operand is stored in the object specified by the first operand, and compound assignment, in which an arithmetic, shift, or bitwise operation is performed prior to storing the result. All assignment operators in the following table except the = operator are compound assignment operators.
| Assignment Operators | |
|---|---|
| Operator | Meaning |
|
= |
Store the value of the second operand in the object specified by the first operand (simple assignment). |
|
*= |
Multiply the value of the first operand by the value of the second operand; store the result in the object specified by the first operand. |
|
/= |
Divide the value of the first operand by the value of the second operand; store the result in the object specified by the first operand. |
|
%= |
Take modulus of the first operand specified by the value of the second operand; store the result in the object specified by the first operand. |
|
+= |
Add the value of the second operand to the value of the first operand; store the result in the object specified by the first operand. |
|
?= |
Subtract the value of the second operand from the value of the first operand; store the result in the object specified by the first operand. |
|
<<= |
Shift the value of the first operand left the number of bits specified by the value of the second operand; store the result in the object specified by the first operand. |
|
>>= |
Shift the value of the first operand right the number of bits specified by the value of the second operand; store the result in the object specified by the first operand. |
|
>>>= |
Unsigned bit shift right the value of the first operand right the number of bits specified by the value of the second operand; store the result in the object specified by the first operand. |
|
&= |
Obtain the bitwise AND of the first and second operands; store the result in the object specified by the first operand. |
|
^= |
Obtain the bitwise exclusive OR of the first and second operands; store the result in the object specified by the first operand. |
|
|= |
Obtain the bitwise inclusive OR of the first and second operands; store the result in the object specified by the first operand. |
Simple Assignment Operator =
At run time, the left operand is evaluated first, resulting in a variable. Next the right operand is evaluated and the result of a simple assignment expression is the value stored into the variable.
The simple assignment operator = sets the lefthand operand to the value of the righthand operand. The right operand must be an expression of a type that is implicitly convertible to the type of the left operand. For example, assign a value to a variable. The equal sign can be used with both primitive values and objects. If the operands are object references, then a copy of the reference value will be assigned -- not the object body.
The assignment operator is evaluated from right to left, so a = b = c = 0;
would assign 0 to c, then c to b then b to a.
The assignment operator has the lowest precedence, allowing the expression on the right-hand side to be evaluated before assignment.
The processing of a simple assignment of the form x = y follows these rules:
-
If the left-hand operand x is evaluated and is a Class (e.g., reference type) variable, the right-operand y is evaluated and must be either a null, or of the same class or subclass type as the class of the left-operand x.
For example:
class A { } class B extends A {} A a = new B(); -
If the left-hand operand x is evaluated and is an interface variable, the right-operand is evaluated and must be either a null, or of the same interface or superinterface of the left-operand, or, a class that implements the interface or it's superinterface.
For example:
interface A { } interface B extends A {} class C implements B {} A a = new C(); -
If the variable given by x is an array element of a reference-type, a run-time check is performed to ensure that the value computed for y is compatible with the array instance of which x is an element. It must be a null, or if an implicit reference conversion exists from the actual type of the instance referenced by y to the actual element type of the array instance containing x.
String[] ss = new String[3]; Object[] os = ss; os[0] = new String("Hello"); os[1] = "Hello"; os[2] = new ArrayList(); //run-time exeception
Compound Assignment Operators
Java also provides several short cut assignment operators that allow you to perform an arithmetic, logical, or bitwise operation and an assignment operation all with one operator.
A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (E1) op (E2), except that E1 is evaluated only once.
In another words, a compound assignment consists of two operands, one on either side of the equal sign (=), in combination with another binary operator. For example:
E1 += E2;
This is equivalent to the following simple assignment (except that in the compound assignment E1 is evaluated once, while in the simple assignment E1 is evaluated twice):
E1 = E1 + E2;