Incremental Java
Side Effects

Side Effects

The goal of evaluating an expression is to compute a value.

Do the values of the variables change? For example, if you're evaluating x * (y + 2) and x has value 3 and y has value 4 (assume both are int), what happens after evaluation?

We know that the evaluation produces 18. But does x and y still remain 3 and 4?

The answer is yes!

For the examples we've seen, expression evaluation is side-effect free. In other words, all it does is to compute a final value. It doesn't change the values of the variables.

Later on, we'll see operators that have side effects. This means that evaluation not only computes a value, but also alters the value of a variable. In general, we want to avoid doing this, though programmers do this for convenience. You just have to be careful.

In general, a side effect is something that happens other than computing a value. So far, the only side effect we've seen is having a variable's value change while an expression is being evaluated.

There are other side effects beyond this, which we'll discuss as we get to them.