Assignment Statement Syntax

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

What does the program print to the monitor?

Answer:

The variable contains: 123

Assignment Statement Syntax

This program prints out the same thing as the first example program. However, this program did not initialize the variable and so had to put a value into it later.

Assignment statements look like this:

variableName  =  expression ;
  • The equal sign = is the assignment operator.
  • variableName is the name of a variable that has been declared previously in the program.
  • expression is a collection of characters that calls for a value.

Here are some example assignment statements (assume that the variables have already been declared):

total = 3 + 5;

price = 34.56;

tax = total*0.05;

In the source file, the variable must be declared before any assignment statement that uses that variable.

QUESTION 11:

Is the following correct:

int sum;

sum = 42 - 12 ;