Arithmetic Expressions

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

What do you suppose is the value of the arithmetic expression:

data[2] + data[6]

Answer:

23; data[2] contains a 14 and data[6] contains a 9, the sum is 23.

Arithmetic Expressions

An expression such as data[3] is called a subscripted variable. A subscripted variable can be used anywhere an ordinary variable of the same type can be used. Since data[3] contains an int it can be used anywhere an int variable may be used.

An arithmetic expression can contain a mix of literals, variables, and subscripted variables. For example, if x contains a 10, then

(x + data[2]) / 4

evaluates to (10+14) / 4 == 6. Here are some other legal statements:

data[0] = (x + data[2]) / 4 ;

data[2] = data[2] + 1;

x = data[3]++ ;       // the data in cell 3 is incremented

data[4] = data[1] / data[6];

QUESTION 4:

Assume that the array holds values as in the picture. What will be the result of executing the statement:

data[0] = data[6] + 8;