Java Reference
In-Depth Information
Note that an assignment is different from initializing a variable in a declaration. Initialization causes a
variable to have the value of the constant that you specify when it is created. An assignment involves copy-
ing data from one place in memory to another. For the preceding assignment statement, the compiler alloc-
ates some memory (4 bytes) to store the constant 777 as type
int
. This value is then copied to the variable
c
. The value in
c
is extracted and copied to
b
. Finally the value in
b
is copied to
a
. (However, strictly speak-
ing, the compiler may optimize these assignments when it compiles the code to reduce the inefficiency of
performing successive assignments of the same value in the way I have described.)
With simple assignments of a constant value to a variable of type
short
or
byte
, the constant is stored as
the type of the variable on the left of the
=
, rather than type
int
. For example:
short value = 0;
value = 10;
Here you have a declaration statement for the variable
value
, followed by an assignment statement.
When the declaration executes, it allocates space for the variable
value
and arranges for its initial value to
be 0. The assignment statement that follows the declaration statement needs to have 10 available as an in-
teger literal of type
short
, occupying 2 bytes, because
value
is of type
short
. The compiler converts the
literal value 10, which is of type
int
, to type
short
. The value 10 is then copied to the variable
value
.
Now let's look in more detail at how you perform calculations with integers.
Integer Calculations
The basic operators you use in calculations involving integers are
+
, −,
*
, and
/
, and these have the usual
meanings — add, subtract, multiply, and divide, respectively. (The
%
operator is also a basic operator and I'll
discuss that later in this chapter.) Each of these is a
binary
operator; that is, they combine two operands to
produce a result. 2 + 3, for example, results in 5. An
operand
is just the term for a value to which an operator
is applied. The priority or precedence that applies when an expression using these operators is evaluated is
the same as you learned at school, so multiplication and division operations are executed before any addition
or subtraction. Evaluating the expression
20 - 3 * 3 - 9 / 3
produces the value 8 because it is equivalent to 20 − 9 − 3.
As you also have learned in school, you can use parentheses in arithmetic calculations to change the se-
quence of operations. Expressions within parentheses are always evaluated first, starting with the innermost
set of parentheses when they are nested. You use parentheses to override the default sequence of operations.
Therefore the expression
(20 -3) x (3 -9) / 3
is equivalent to 17 * (−6) / 3, which results in −34.
Of course, you use these operators with variables that store integer values as well as integer literals. You
could calculate a value to be stored in a variable,
area
, of type
int
from values stored in the variables
length
and
width
, also of type
int
, by writing the following:
area = length * width;