Variables and Assignment Statements

Read this chapter, which covers variables and arithmetic operations and order precedence in Java.

10. Assignment Statement Syntax


Answer:

The variable contains: 123

The 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 Statement Syntax

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 to be calculated.

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 ;