Assignment operators

Figure 124. The += operator Slide presentation

Increment variable by right hand value:

int a = 4;

a = a + 2;

System.out.println("Value:" + a);
int a = 4;

a += 2;

System.out.println("Value:" + a);
Value:6

exercise No. 45

Strange addition

Q:

Execute the following snippet and explain its result:

byte b = Byte.MAX_VALUE;
System.out.println(b);
b += 1;
System.out.println(b);

A:

In binary representation starting from Byte.MAX_VALUE we'd see:

  01111111         127 
 +       1       +   1
  --------        ----
  10000000         128

But a byte variable is being represented as 1-byte 2 complement:

  01111111         127 
 +       1       +   1 Overflow!
  --------        ----
  10000000        -128

Note that assigning b = b + 1 yields a compile time error instead:

Required type: byte
Provided: int

Conclusion: the += operator leads to a cyclic behaviour and thus differs from the ordinary + operator.

Figure 125. The &= operator Slide presentation

Logical and operation:

boolean examSuccess = true,
        registered = false;

examSuccess = examSuccess & registered;

System.out.println(
  "Exam success:" + examSuccess);
boolean examSuccess = true,
        registered = false;

examSuccess &= registered;

System.out.println(
  "Exam success:" + examSuccess);
Exam success:false

Figure 126. Arithmetic assignment operators Slide presentation
= Assign right to left operand Example using %=
+= Assign sum of operands to left operand
int value = 13;
value %= 5;
System.out.println("Result=" + value);
-= Assign difference of operands to left operand
*= Assign product of operands to left operand
/= Assign quotient of operands to left operand
%= Assign remainder of operands to left operand
Result=3

Figure 127. Logical assignment operators Slide presentation
&= Assign logical and of operands to left operand
|= Assign logical or of operands to left operand

exercise No. 46

Understanding +=

Q:

Consider the following snippet:

byte age = 80;

age += 2;

This will compile and execute thereby incrementing the age variable's value by 2 as expected. The seemingly equivalent code however will not even compile:

byte age = 80;

age = age + 2; // Error: Incompatible types.
               // Required: byte
               // Found: int

On the other hand the += operator even accepts values exceeding a byte's upper range limit of 127:

byte age = 80;

age += 200;

So why is age += 2 perfectly legal on contrary to age = age + 2 ?

A:

The Java® Language Specification SE 19 Edition offers a definition in its Compound Assignment Operators section:

A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T) ((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once

We provide an example illustrating this rather condensed statement:

double d = 4.5;
byte i = 3;

    i  +=  d  ;  
// E1 op= E2

We thus link:

  • i to E1

  • the + operator to op

  • d to E2

Our variable i aka E1 is of type int. T being E1's type the expression E1 = (T) ((E1) op (E2)) hence translates to:

i = (int)(i + d);

Back to our original example: According to Figure 120, “No binary + operator yielding byte the arithmetic + operator acting any two non-long values will always return a result of type int. The expression age + 2 (The literal 2 is of type int anyway) is thus of type int and therefore requires a cast when being assigned to our variable age of type byte:

byte age = 80;

age = (byte)(age + 2);

Note

Since age is not being defined as final the value of the expression age + 2 is not being known at compile time. Adding final would allow for compile time evaluation and thus assignment to a second variable of type byte without requiring a cast:

final byte age = 80;         // Value cannot be altered.

byte gettingOlder = age + 2; // o.K. without cast: age + 2 can be evaluated at compile time.

On contrary regarding the age += 2 expression the equivalent is age = (byte)(age + 2):

Code Execution result
byte age = 80;
age += 200;
System.out.println("Age:" + age);
Age:24

Notice 24 being equal to 80 + 200 - 256 with 256 being a byte's number of different representable values.