Java Reference
In-Depth Information
Generally you want to prevent the values of these variables from being modified. Accidental changes to
the number of feet in a yard could make the results of your program suspect, to say the least. Java provides
you with a way to fix the value of any variable by using the final keyword when you declare it, for ex-
ample:
final int FEET_PER_YARD = 3; // Constant values
final double MM_PER_INCH = 25.4; // that cannot be changed
The final keyword specifies that the value of a variable is final and must not be changed. The compiler
checks your code for any violations of this and flags them as errors. I've used uppercase letters for the names
of the variables here because it is a convention in Java to write constants in this way. This makes it easy to
see which variables are defined as constant values. Obviously, any variable you declare as final must have
an initial value assigned as you can't specify it later.
Now that you know how to declare and initialize variables of the basic types, you are nearly ready to
write a program. You just need to look at how you express the calculations you want carried out, and how
you store the results.
ARITHMETIC CALCULATIONS
You store the result of a calculation in a variable by using an assignmentstatement . An assignment statement
consists of three elements: the name of the variable where you want the result stored; the assignment oper-
ator, =, that indicates that this is indeed an assignment operation; and an arithmetic expression that defines
the calculation you want to perform to produce the result. The whole thing is terminated by a semicolon that
marks the end of the assignment statement. Here's a simple example of an assignment statement:
numFruit = numApples + numOranges; // Calculate the total fruit
When this statement executes, the value of the expression to the right of the assignment operator, =, is
calculated, and the result is stored in the variable that appears to the left of the = sign. In this case, the values
stored in the variables numApples and numOranges are added together, and the result is stored in the vari-
able numFruit . Of course, you have to declare and initialize all three variables before using this assignment
statement.
Incrementing a variable by a given amount is a common requirement in programming. Look at the fol-
lowing assignment statement:
numApples = numApples + 1;
The result of evaluating the expression on the right of the = is one more than the value of numApples .
This result is stored back in the variable numApples , so the overall effect of executing the statement is to
increment the value in numApples by 1. You will see an alternative, more concise, way of producing the
same effect shortly.
You can write multiple assignments in a single statement. Suppose you have three variables a , b , and c
that you have defined to be of type int , and you want to set all three to 777. You can do this with the fol-
lowing statement:
a = b = c = 777;
Search WWH ::




Custom Search