Assignment Statement Symantics

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   hear noise   go to next page
int sum;
sum = 42 - 12 ;

Answer:

Yes, the statements are syntactically correct.

Assignment Statement Symantics

The syntax of a programming language says what programs look like. It is the grammar of how to arrange the symbols. The symantics of a programming language says what the program does as it executes. It says what the symbols mean.

This page explains the symantics of the assignment statement. An assignment statement asks for the computer to perform two steps, in order:

An assignment statement does its work in two steps:

  1. First, it does the calculation on the RIGHT of the equal sign.
    • This means it calculates the value of the expression on the right.
    • If there is nothing to calculate, it just uses the value that is there.
  2. Next, it replaces the contents of the variable to the LEFT of the equal sign with the result of the calculation.

For example:

total = 3 + 5;
  1. Does the calculation 3+5 and gets 8.
  2. Then puts the 8 in the variable named total.

It does not matter if total already has a number in it. Step 2 will replace whatever is already in total .

QUESTION 12:

What happens FIRST when the following statement executes?

value = 2*3 ;