Assignment Operator

Purpose:

To assign a value to a variable.

Mechanics:

<variable name> = <expression>;

Example:

int myNumber;
myNumber = 1;

Usage:

  • <variable name> is a variable or parameter defined in the current scope.
  • <expression> is a variable, function, or parameter -- or any combination of them, with operators and parentheses -- that evaluates to the same type as <variable name>. The <expression> on the right is evaluated. If <expressions> has multiple parts, as in a chain of arithmetic operations, then it is evaluated according to the heirarchy of operations.
  • The final result of <expression> is stored in <variable name>.

Restrictions:

If <variable name> has been declared final, then <variable name> can only be assigned a value at declaration. All subsequent attempts to assign a value to <variable name > are compile time errors.
Back to the Table of Contents
email suggestions to: cs015tas@cs.brown.edu