Java Reference
In-Depth Information
able is accessible at a given point in a program, and we look deeper into the significance of this in the next
chapter. Broadly, you should group related variable declarations together, immediately before the code that
uses them.
You can declare and define multiple variables in a single statement. For example:
long bigOne = 999_999_999L, largeOne = 100_000_000L;
Here I have declared two variables of type long . A comma separates each variable from the next. You
can declare as many variables as you like in a single statement, although it is usually better to stick to de-
claring one variable in each statement as it helps to make your programs easier to read. A possible exception
occurs with variables that are closely related — an (x,y) coordinate pair representing a point, for example,
which you might reasonably declare as the following:
int xCoord = 0, yCoord = 0; // Point coordinates
On the same line as the declaration of these two variables, I have a comment following the double slash,
explaining what the variables are about. The compiler ignores everything from the double slash (//) until the
end of the line. Explaining the purpose of your variables in comments is a good habit to get into, as it can be
quite surprising how something that was as clear as crystal when you wrote it transmogrifies into something
as clear as mud a few weeks later. You can add comments to your programs in other ways that you see a
little later in this chapter.
You can also spread a single declaration over several lines if you want. This also can help to make your
program more readable. For example:
int miles = 0, // One mile is 8 furlongs
furlongs = 0, // One furlong is 220 yards
yards = 0, // One yard is 3 feet
feet = 0;
This defines four variables of type int in a single statement with the names miles , furlongs , yards ,
and feet . Each variable has 0 as its initial value. Naturally, you must be sure that an initializing value for
a variable is within the range of the type concerned; otherwise, the compiler complains. Your compiler is
intelligent enough to recognize that you can't get a quart into a pint pot or, alternatively, a very large long
constant into a variable of type int , short , or byte . Because the statement is spread over four lines, I am
able to add a comment on each of the first three lines to explain something about the variable that appears
on it.
To complete the set of variables that store integers, you can declare and initialize a variable of type byte
and one of type short with the following two statements:
byte luckyNumber = 7;
short smallNumber = 1234;
Here the compiler can deduce that the integer literals are to be of type byte and short , respectively, and
can convert the literals to the appropriate type. It is your responsibility to make sure the initial value fits
within the range of the variable that you are initializing. If it doesn't, the compiler rejects the statement and
outputs an error message.
Most of the time you will find that variables of type int will cover your needs for dealing with integers,
with type long being necessary now and again when you have some really big integer values to deal with.
Search WWH ::




Custom Search