Java Reference
In-Depth Information
long bigOne = 999999999L, largeOne = 100000000L;
Here we 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 declaring 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:
int xCoord = 0, yCoord = 0; // Point coordinates
On the same line as the declaration of these two variables, we have a comment following the double
slash, explaining what they are about. The compiler ignores everything from the double slash until the
end of the line. Explaining in comments what your variables are for 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. There are other ways in which you can add comments to
your programs that we will 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;
Naturally, you must be sure that an initializing value for a variable is within the range of the type
concerned, otherwise the compiler will complain. Your compiler is intelligent enough to recognize that
you can't get a quart into a pint pot, or, alternatively, a long constant into a variable of type int ,
short , or byte .
To complete the set we 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 convert the literals to the appropriate type. It is your responsibility to make sure the initial value
will fit within the range of the variable that you are initializing. If it doesn't the compiler will throw it
out with an error message.
Most of the time you will find that variables of type int will cover your needs for dealing with integers,
with long ones being necessary now and again when you have some really big integer values to deal
with. Variables of type byte and short do save a little memory, but unless you have a lot of values of
these types to store, that is, values with a very limited range, they won't save enough to be worth
worrying about. They also introduce complications when you use them in calculations, as we shall see
shortly, so generally you should not use them unless it is absolutely necessary. Of course, when you are
reading data from some external source, a disk file for instance, you will need to make the type of
variable for each data value correspond to what you expect to read.
Search WWH ::




Custom Search