Integer Data Types
There are four types of variables that you can use to store integer data. All of these are signed, that is, they can store both negative and positive values. The four integer types differ in the range of values they can store, so the choice of type for a variable depends on the range of data values you are likely to need.

The four integer types in Java are:

Data Type
Description

byte
Variables of this type can have values from -128 to +127 and occupy 1 byte (8 bits) in memory

short
Variables of this type can have values from -32768 to 32767 and occupy 2 bytes (16 bits) in memory

int
Variables of this type can have values from -2147483648 to 2147483647 and occupy 4 bytes (32 bits) in memory

long
Variables of this type can have values from -9223372036854775808 to 9223372036854775807 and occupy 8 bytes (64 bits) in memory


Let's take a look at declarations of 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, as shown in the table above, is always the same, regardless of what kind of computer you are using. This is also true of the other basic types that we will see later in this chapter, and has the rather useful effect that your program will execute in the same way on computers that may be quite different. This is not necessarily the case with other programming languages.

Of course, although we have expressed the range of possible values for each type as decimal values, integers 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 on the next page.


For each of the binary numbers shown here, 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. Binary negative numbers are represented in what is called 2's complement form. If you are not familiar with this, you will find an explanation of how it works in Appendix B.

Integer Values
An integer variable stores an integer value, so before we get to use integer variables we need to investigate how we write various integer values. As we said earlier, a value of any kind in Java is referred to as a literal. So 1, 10.5, and "This is text" are all examples of literals.

Any integer literal that you specify is of type int by default. Thus 1, -9999, and 123456789 are all literals of type int. If you want to define an integer of type long, and the value that you assign to the variable is bigger than an int, you need to append an L to the value. The values 1L, -9999L, and 123456789L are all of type long. You can also use a lower case letter l, but don't - it is too easily confused with the digit 1.

You are perhaps wondering how you specify literals of type byte or short. Because of the way integer arithmetic works in Java, they just aren't necessary in the main. We will see a couple of instances where an integer literal may be interpreted by the compiler as type byte or short later in this chapter, but these situations are the exception.

Integer literals can also be specified to base 16, in other words, as hexadecimal numbers. Hexadecimal literals in Java have 0x or 0X in front of them and follow the usual convention of using the letters A to F (or a to f) to represent digits with values 10 to 15 respectively. In case you are a little rusty on hexadecimal values, here are some examples:

0x100
1*162 + 0*161 + 0*160
which is 256 in decimal

0x1234
1*163 + 2*162 + 3*161 + 4*160
which is 4660 in decimal

0xDEAF
13*163 + 14*162 + 10*161 + 15*160
which is 57007 in decimal

0xCAB
12*162 + 10*161 + 11*160
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:

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.
_________________________
My New site OpenEyes