Java Reference
In-Depth Information
1*16 2 + 0*16 1 + 0*16 0
0x100
which is 256 in decimal
1*16 3 + 2*16 2 + 3*16 1 + 4*16 0
0x1234
which is 4660 in decimal
13*16 3 + 14*16 2 + 10*16 1 + 15*16 0
0xDEAF
which is 57007 in decimal
0xCAB
12*16 2 + 10*16 1 + 11*16 0
which is 3243 in decimal
If you are not familiar with hexadecimal numbers, you can find an explanation of how these work in
Appendix B.
There is a further possibility for integer constants - you can also define them as octal, which is to base 8.
Octal numbers have a leading zero so 035 and 067 are examples of octal numbers. Each octal digit defines
three bits, so this number base was used a lot more frequently in the days when machines used a multiple of
three bits to store a number. You will rarely find it necessary to use octal numbers these days, but you should
take care not to use them by accident. If you put a leading zero at the start of an integer literal, the Java
compiler will think you are specifying an octal value. Unless one of the digits is greater than 7, which will
result in the compiler flagging it as an error, you won't know that you have done this.
Declaring Integer Variables
As you saw earlier, we can declare a variable of type long with the statement:
long bigOne;
This statement is a declaration for the variable bigOne . This specifies that the variable bigOne will
store a value of type long . When this statement is compiled, 8 bytes of memory will be allocated for
the variable bigOne . Java does not automatically initialize a variable such as this. If you want your
variables to have an initial value rather than a junk value left over from when the memory was last used,
you must specify your own value in the declaration. To declare and initialize the variable bigOne to
2999999999 , you just write:
long bigOne = 2999999999L;
The variable will be set to the value following the equal sign. It is good practice to always initialize your
variables when you declare them. Note that if you try to use a variable in a calculation that has not had
a value assigned to it, your program will not compile. There are also circumstances where the compiler
cannot determine whether or not a variable has been initialized before it is used if you don't initialize it
when you declare it, even though it may be obvious to you that it has been. This will also be flagged as
an error but getting into the habit of always initializing variables when you declare them will avoid all of
these problems.
You can declare a variable just about anywhere in your program, but you must declare a variable before
you use it in a calculation. The placement of the declaration therefore has an effect on whether a
particular variable is accessible at a given point in a program, and we will look deeper into the
significance of this in the next chapter. Broadly, you should group related variable declarations
together, before the block of code that uses them.
You can declare and define multiple variables in a single statement. For example:
Search WWH ::




Custom Search