Home Segments Top Top Previous Next

425: Sidetrip

Although augmented assignment operators are not used further in this book, you should know that an expression written with an augmented assignment operator may execute faster than the corresponding expression written without the augmented assignment operator. In Chapter 28, for example, you learn about Java arrays. In particular, you learn that you can reassign an array element to twice its former value as follows:

array name [index-producing expression]  
  = array name [index-producing expression] * 2 

Alternatively, using an augmented assignment operator, you can write the reassignment expression as follows:

array name [index-producing expression] *= 2 

Plainly, if the index-producing expression is complex, the augmented assignment operator offers one way to increase speed, to keep size down, and to avoid a maintenance headache should the expression require modification, because the index-producing expression is written only once and is evaluated only once.