Java Reference
In-Depth Information
Basic Assignment
Identifier
=
Expression
;
The basic assignment statement uses the assignment operator (=) to
store the result of the Expression into the specified Identifier, usually
a variable.
Examples:
total = 57;
count = count + 1;
value = (min / 2) * lastValue;
When a reference is made to a variable, such as when it is printed, the value
of the variable is not changed. This is the nature of computer memory: Accessing
(reading) data leaves the values in memory intact, but writing data replaces the
old data with the new.
The Java language is
strongly typed, meaning that we are not
allowed to assign a value to a variable that is inconsistent with its
declared type. Trying to combine incompatible types will generate
an error when you attempt to compile the program. Therefore, the
expression on the right-hand side of an assignment statement must
evaluate to a value compatible with the type of the variable on the left-hand side.
KEY CONCEPT
We cannot assign a value of one type
to a variable of an incompatible type.
Constants
Sometimes we use data that is constant throughout a program. For instance, we
might write a program that deals with a theater that can hold no more than 427
people. It is often helpful to give a constant value a name, such as MAX_OCCUPANCY ,
instead of using a literal value, such as 427, throughout the code. The purpose
and meaning of literal values such as 427 is often confusing to someone reading
the code. By giving the value a name, you help explain its role in the program.
Constants are identifiers and are similar to variables except that
they hold a particular value for the duration of their existence.
Constants are, to use the English meaning of the words, not variable.
Their value doesn't change.
In Java, if you precede a declaration with the reserved word final , the identi-
fier is made a constant. By convention, uppercase letters are used when naming
constants to distinguish them from regular variables, and individual words are
KEY CONCEPT
Constants hold a particular value for
the duration of their existence.
 
Search WWH ::




Custom Search