1.5 Other Primitive Types

In addition to the four integer types discussed in the previous section, Java has four other primitive types of values: float, double, char, and boolean. We will examine them in this section.

To store numeric values containing decimals or to store integers whose size exceeds the largest value that can be stored in a long integer, Java uses two floating point types: float and double. In both of these types, the numbers are represented in a form that is similar to scientific notation. In scientific notation, the value 576.34 is written in the form 5.7634 x 102. In a floating point representation, the number is separated into two parts: the mantissa (the digits) and the exponent. The next table gives a summary of the two floating point types.

Floating point constants in Java can be written as an optional plus or minus sign, followed by a sequence of one or more digits together with a decimal point. A floating point constant is normally stored as a double value. If the letter F or f is placed at the end of a constant, it is stored as a float value. In the past, memory was more costly and operations on float values were much faster than those on double values. The situation has improved since then and now the type double is almost always used for floating point values. Although double values can represent a wide range of numbers to a very high precision, you should realize that they cannot represent every real number; to do so would require an infinite number of bits. Instead, during calculations, values are rounded if necessary. Although rounding errors can become critical in some situations, for our purposes, double values will be adequately precise.
Example 1

Each of the following are valid floating point constants.

    5.23    .3    2818.    -0.0002    6.7f

The first four would be stored as double values while the last one would be stored as a float value.

Floating point constants can also be written in a form similar to that used in scientific notation with one part showing the digits and the other showing the position of the decimal point. The system is illustrated in the next example.

Example 2

(a) 5.6e2     represents the value 5.6 X 102 = 560
(b) 37E-4     represents the value 37 X 10-4 = 0.0037
(c) -0.667e-2  represents the value -0.667 X 10-2 = -0.00667

Groups of bits can also be used to represent characters. Early computers used six bits to represent a character, permitting a total of 26 = 64 different characters. With this scheme, only the 10 digits, 26 upper case letters of the alphabet, and a variety of punctuation marks and other symbols could be represented. The details of the coding scheme varied from computer to computer. More recently, a seven bit encoding called ASCII (American Standard Code for Information Interchange) has gained wide acceptance, allowing the use of both upper and lower case characters and far more special characters.

The creators of Java have chosen a newer representation called Unicode that uses 16 bits to represent each character. With 16 bits, a total of 216 = 65 536 characters can be represented. With such a large number of character codes available, the characters and symbols used by any of the world's languages along with a wide variety of symbols and shapes can be represented by a character in Unicode. You need not be concerned about the details of the Unicode encoding scheme but, if you are interested, look at http://unicode.org/.

Characters in Java programs are of type char. We show constants representing single characters by enclosing them in apostrophes (also known as single quotes).

Example 3

The following are all valid char constants.

            'A'    '$'    '+'    '7'

To represent the apostrophe character, we use an escape sequence, just as we did inside strings (in the previous section). Thus, the constant that represents the single apostrophe can be written in the form '\''. Similarly, the newline character (that causes a jump to a new line) can be written as '\n'.

It is also possible to represent a character by specifying the bit pattern as a hexadecimal value in a Unicode escape sequence. For example, the character Σ can be specified by writing \u3a03. If you are interested in exploring this further, see Appendix C. You do not need to know anything about this form of character representation in order to follow any of the material in this text.

The last of Java's eight primitive types is called boolean. The boolean type represents a truth value. There are only two boolean values: true and false. These words, true and false are reserved by Java for boolean values and cannot be used in any other way. Boolean values are represented using one bit.

Exercise 1.5

    
    1.Rewrite in standard decimal form.
      (a) 2.94e1        (b) 0.0004E3
      (c) -2e-3         (d) 26.77e-3
      (e) -54E-3        (f) -.3e1
    
    2. The double constants 6.73 and 67.3e-1 would be stored identically in Java. Find three other representations
       of each of the following double values.
      (a) 27E4          (b) -0.0058e2
      (c) -87.69        (d) -.3e-4
    
    3. Identify the type of each constant.
      (a) -3.8e2        (b) 53L
      (c) -12           (d) false
      (e) 17 .4         (f) 2E3
      (g) .5f           (h) '2'