Java Reference
In-Depth Information
DATA
TYPE
DESCRIPTION
Variables of this type can have values from −32768 to 32767 and occupy 2 bytes (16 bits) in memory
short
Variables of this type can have values from −2147483648 to 2147483647 and occupy 4 bytes (32 bits) in
memory
int
Variables of this type can have values from −9223372036854775808 to 9223372036854775807 and occupy
8 bytes (64 bits) in memory
long
Although I said that the choice of type depends on the range of values that you want to be able to store,
in practice you'll be using variables of type int or type long to store integers most of the time, for reasons
that I explain a little later. Let's take a look at declarations for variables of each of these types:
byte smallerValue;
short pageCount;
int wordCount;
long bigValue;
Each of these statements declares a variable of the type specified.
The range of values that can be stored by each integer type in Java is shown in the preceding table and
this is always the same, regardless of what kind of computer you are using. This is also true of the other
primitive types that you will see later in this chapter. This has the rather useful effect that your program
executes in the same way on computers that might be quite different. This is not necessarily the case with
other programming languages.
Of course, although I have expressed the range of possible values for each type as decimal values, in-
tegers are stored internally as binary numbers, and it is the number of bits available to store each type that
determines the maximum and minimum values, as shown in Figure 2-1 .
FIGURE 2-1
For each of the binary numbers shown in Figure 2-1 , the leftmost bit is the sign bit, marked with an s .
When the sign bit is 0 the number is positive, and when it is 1 the number is negative. Negative binary num-
bers are represented in what is called 2 ' s complement form . If you are not familiar with this, you can find an
explanation of how it works in Appendix B.
 
 
Search WWH ::




Custom Search