Experiment 1: Simple Expressions


 

Study the source program Express.java, particularly the lines

  theScreen.println("\n"  +  i  +  '+'  +  j  +  " = "  +  (i + j));
  theScreen.println("\n"  +  x  +  '+'  +  y  +  " = "  +  (x + y));

In the space below, write what you think will be appear on the screen when this program is executed (don't worry, wrong guesses are not penalized.)





Compile Express.java, and run (execute) the resulting program. What is displayed?





If what was displayed was what you anticipated, give a detailed explanation of why you said what you did. If what was displayed differs from what you anticipated, explain what was wrong with your thinking.





From this, we can see that the effect of the output statement is to display on the screen the value of each Expression in the statement.

Simple Expressions

An expression that has a single operand and no operators is called a simple expression. Two of the sub-expressions in the output statements in Express.java are not simple expressions. Which of the sub-expressions are not simple expressions?





This example illustrates several different kind of simple expressions:

 

Output Expressions

The final thing to see in this exercise is that the output statement uses a not-so-simple expression. That is, in the lines below

  theScreen.println("\n"  +  i  +  '+'  +  j  +  " = "  +  (i + j));
  theScreen.println("\n"  +  x  +  '+'  +  y  +  " = "  +  (x + y));

"\n" + i + '+' + j + " = " + (i + j) is a string expression which is evaluated and then printed on your screen.


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.