The Java Course provides a general introduction to programming in Java. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Multiple Assignment

I haven't said much about it, but it is legal in Java to make more than one assignment to the same variable. The effect of the second assignment is to replace the old value of the variable with a new value.

    int fred = 5;
    System.out.print (fred);
    fred = 7;
    System.out.println (fred);

The output of this program is 57, because the first time we print fred his value is 5, and the second time his value is 7.

This kind of multiple assignment is the reason I described variables as a container for values. When you assign a value to a variable, you change the contents of the container, as shown in the figure:

When there are multiple assignments to a variable, it is especially important to distinguish between an assignment statement and a statement of equality. Because Java uses the = symbol for assignment, it is tempting to interpret a statement like a = b as a statement of equality. It is not!

First of all, equality is commutative, and assignment is not. For example, in mathematics if a = 7 then 7 = a. But in Java a = 7; is a legal assignment statement, and 7 = a; is not.

Furthermore, in mathematics, a statement of equality is true for all time. If a = b now, then a will always equal b. In Java, an assignment statement can make two variables equal, but they don't have to stay that way!

    int a = 5;
    int b = a;     // a and b are now equal
    a = 3;         // a and b are no longer equal

The third line changes the value of a but it does not change the value of b, and so they are no longer equal. In many programming languages an alternate symbol is used for assignment, such as <- or :=, in order to avoid this confusion.

Although multiple assignment is frequently useful, you should use it with caution. If the values of variables are changing constantly in different parts of the program, it can make the code difficult to read and debug.



Last Update: 2011-01-24