Lab 3: Java Expressions



Today's exercise involves a series of experiments, each investigating a different aspect of a fundamental Java concept: the expression. The term expression can be defined as follows:

An expression is a sequence of one or more operands, and zero or more operators, that when combined, produce a value.

To illustrate, the sequence


fits the definition of an expression, since it consists of one operand (12) and zero operators that combine to produce a value (12). Perhaps more familiarly, the sequence

   2 + 3

fits the definition of an expression, since it consists of two operands (2 and 3) and one operator (+) that combine to produce a value (5). Operands need not be constants: the sequence

   2.5 * x - 1.0

fits the definition of an expression, since it consists of three operands (2.5, x and 1.0) and two operators (*, -) that combine to produce a value (1 less than the product of 2.5 and x).

These last two examples have been arithmetic expressions, that is, expressions whose operators are familiar arithmetic operators. As we shall see in today's exercise, Java provides a rich set of both arithmetic and non-arithmetic operators that allow Java programmers to construct expressions.


The Experimental Laboratory

Using what you have learned previously, begin by creating an Express project as appropriate for your environment.

We will use the file as a software laboratory for most of the experiments in this exercise. Click on the preceding link, and use your browser's

   File -> Save As

menu choice to save this file in your Express project directory. Add to your project if needed. You can then open the file and take a moment to study its contents.

In our print() and println() statements, we have expressions of the form

  Expression1 + Expression2 + ... + ExpressionN;

where some of the expressions are characters or strings. The exact details are fairly complicated, but the net effect is that Expression1 is evaluated and displayed, after which Expression2 is evaluated and displayed, after which ..., after which ExpressionN is evaluated and displayed. An output statement thus provides a simple "laboratory" in which we can "experiment" with expressions by building them and viewing the values they produce. Note that the parentheses around the i + j are required if we actually want to print the sum of i and j and not the value of i followed by the value of j.


Below is a list of the experiments available for this exercise. Your instructor will tell you which ones you can omit, if any.

For each experiment that you are to perform, click its link and print a hard copy of the resulting web page. Then record your experimental results on that hard copy.

Experiment 1: Simple Expressions

Experiment 2: Characters and Character Strings

Experiment 3: Arithmetic Expressions

Experiment 4: Relational Expressions

Experiment 5: Logical Expressions

Experiment 6: Operator Precedence

Experiment 7: Operator Associativity

Experiment 8: Expressions Containing Functions

Experiment 9: Declarations

Experiment 10: Constant Declarations

Experiment 11: Assignment Statements

Experiment 12: Assignment Associativity

Experiment 13: Assignment Shortcuts

Experiment 14: Increment and Decrement Expressions


Phrases you should now understand:

Expression, Simple Expression, Operand, Operator, Operator Precedence, Operator Associativity, Integer Division, Logical Operator, Boolean Operator, Libraries of Methods, Variable Declaration, Constant Declaration, Operator Chaining, Increment Expression, Decrement Expression.



Turn in to your instructor the hard copies showing the results of each of your experiments, plus a hard copy of your final version of

Back to the Prelab Questions

Back to This Lab's Table of Contents

Forward to the Homework Projects

Back to the Table of Contents

Back to the Introduction

Copyright 2000 by Prentice Hall. All rights reserved.