An assignment statement gives a value to a variable. For example,
gives the value 5.
The value of a variable may be changed. For example, if has the value 5, then the assignment statement
will give the value 6.
The general syntax of an assignment statement is
- the must be declared;
- the may be a simple name, or an indexed location in an array, or a field (instance variable) of an object, or a static field of a class; and
- the expression must result in a value that is compatible with the type of the . In other words, it must be possible to cast the expression to the type of the variable.
An assignment "statement" is not really a statement (although it is typically used that way), but is an expression. The value of the expression is the value that is assigned to the variable. For example, the expression
sets all of , , and to zero.