Experiment 11: Assignment Expressions

Once we are able to declare variables, the next useful operation is to be able to change the value of those variables. This is most commonly done with an assignment statement:

	Variable = Expression ;

Here, Expression is any valid Java statement and Variable is an identifier that has been declared as a variable, whose type matches that of Expression. When execution reaches this statement:

  1. Expression is evaluated; and
  2. Its value is stored in the memory associated with Variable.

In Java, the assignment symbol (=) is an actual operator that returns a value. Put differently, the sequence of symbols

   Variable = Expression

is itself an expression that produces some value, just as

   2 + 3

is an expression that produces some value (5). We might determine what value is produced by this operator by performing an experiment that uses an output statement to display that value:

   theScreen.println ( Variable = Expression );

Using an experiment like this, determine what value is produced by the assignment operator. Use a variety of values for Expression, and report your findings in the space below:

You should now understand that = is not just a syntactic symbol, but is an operator that produces a value (the value being assigned). We will examine the implications of this idea in the next experiment.

Back to the Exercise List

Forward to the Next Experiment

Back to the Table of Contents

Back to the Introduction

Copyright 2000 by Prentice Hall. All rights reserved.