Java Reference
In-Depth Information
decimal integer literals, underscores can only appear between digits in hexadecimal literals so 0x_3ABC
and 0x3ABC_ are both errors.
Binary Literals
It is sometimes convenient to specify an integer literal as a binary value. Placing 0b or 0B in front of a literal
identifies it as a binary number. In this case the digits can only be 0 or 1. For example, 0b110010101011 or
0B110010101011 is the same value as 0xCAB or the decimal value 3243. You can use the underline char-
acter in binary literals too, so you could also write the value as 0b1100_1010_1011, which is much easier to
read. Each group of four binary digits corresponds to one hexadecimal digit. Of course, binary literals can
also be of type long ; you just append an L to the number. 0b_1000 and 0b1000_ are both errors because
underscores can only appear between digits.
Octal Literals
You write literals that are octal numbers with a leading zero so 035 and 067 are examples of octal numbers
of type int and 0777777L is an octal literal of type long . The latter could also be written 0777_777L. Octal
numbers can only use the digits 0 to 7 and each octal digit defines 3 bits. Octal numbers were used fre-
quently in the days when machines used words of lengths that were a multiple of 3 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 thinks you are
specifying an octal value. Unless one of the digits is greater than 7, which results in the compiler flagging it
as an error, you won't know that you have done this and the value will not be what you think it is.
Declaring Integer Variables
As you saw earlier, you 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 stores
a value of type long . When this statement is compiled, 8 bytes of memory are 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 2,999,999,999, you just write:
long bigOne = 2_999_999_999L;
The variable is set to the value following the equal sign. It is good practice to always initialize your vari-
ables when you declare them. I inserted underlines to make the literal easier to read. Note that if you try to
use a variable in a calculation that has not had a value assigned to it, your program does 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 is also flagged as an error, but if you get into the habit of always initializing variables when
you declare them, you can avoid all of these problems.
You can declare a variable just about anywhere in your program, but you must declare each variable be-
fore you use it in a calculation. The placement of the declaration has an effect on whether a particular vari-
Search WWH ::




Custom Search