1.4 Integer Types

The basic unit of information in a computer is called a bit. It can be in one of two states, usually designated as zero and one. Many physical devices can also be in one of two states and, therefore, can be used to store bits of information. As an example, on a compact disc, the presence or absence of a very small pit in thfe disc indicates a one or a zero at a particular point. A group of eight bits is called a byte. Since each bit can be in one of two states, a byte can be in one of 2 x 2 x 2 x 2 x 2 x 2 x 2 x 2 = 28 = 256 states. In Java, a byte can be used to store small integers: 27 negative values (from -1 to -128) and 27 non-negative values (from 0 to 127). Notice that the greatest positive number has a magnitude that is one less than that of the least negative number. This is because zero is one of the 27 non-negative values. An integer value that is stored in one byte is said to be of type byte. The concept of a type is an important feature of high-level computer languages. It allows us to convey meaning to an otherwise meaningless group of bits. In Java, the byte type is a group of eight bits that is to be treated as an integer. If we wish to store an integer value with a larger range than that permitted by the type byte, we can use 16 bits. integer values stored in 16 bits are said to be of type short. Their range is from -215 to 215 - l. Java permits two other types of integer values: int and long. The following table gives a summary of the four integer types.
The int type is adequate for most tasks and you should use it for almost all situations that require an integer. The types byte or short can be used if we need to save space in memory while long can be used if we require integers with an extended range.

Example 1

The integer value 2 000 000 could be represented by an int (maximum value 231 - 1 = 2 147 483 647) but not by a short (maximum value 215 - 1 = 32 767).

Integer constants in Java are written as a sequence of decimal digits with or without a preceding plus or minus sign. They should not be written with any leading zeros (In Java, numbers with leading zeros are not considered to be standard, base 10 numerals. For a detailed explanation of this somewhat bizarre rule, see Appendix C) An integer constant is normally stored as an int value. If the letter L or l is placed at the end of a constant it is stored as a long value.

Example 2

The following are all valid integer constants. The first five would be stored as int values while the last one would be stored as a long value.                                                                                                                                                                               
                                                    4           0           113           -357462           +23            9876543210L

Integer constants must not be written with a decimal point and they cannot contain any separators between digits

Example 3

Each of the following is an illegal integer constant.
  1. 37.0        contains a decimal point.
  2. -12 562     contains a blank between digits.
  3. 1,233,985   contains commas between digits

Exercise 1.4

  1. A group of 4 bits is called a nibble. How many different values could be represented by a nibble?
  2. What is the value of the largest positive integer that can be represented by each of the following types?
    (a) byte      (b) long
    (c) short     (d) int
  3. What is the smallest type of integer value that would be required to store each of the following integer values?
    (a) 50 000    (b) -3000000 000
    (c) -125      (d) 128
  4. State, with reasons, which of the following are not legal Java integer constants.
    (a) -47    (b) 23.
    (c) -0     (d) 22 900
  5. In some computer languages (although not in Java), groups of bits can be used to represent unsigned integers whose least value is zero. In such a representation, what is the largest integer that could be stored as an unsigned integer using 32 bits?