Java has eight primitive data types, shown in Table 26.1:
Type | Size | Range of Values |
---|---|---|
byte | 8 bits | -128 to 127 |
short | 16 bits | -32,768 to 32,767 |
int | 32 bits | -2,147,483,648 to 2,147,483,647 |
long | 64 bits | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
float | 1.4E-45 to 3.4E+38 | |
double | 4.9E-324 to 1.7E+308 | |
char | one character | |
boolean | true or false |
Notice that no String data type is available. The closet thing to a string is the byte data type that represents a single character. Java handles Strings with a class named String. Capitalization is important here. Notice that all the data types appear in lowercase. They must be written that way in the Java code as well. The String class is capitalized to denote that it is a class rather than a data type. All the primitive data types also have associated classes. These classes provide special functions related to the associated data type.
For example, the int data type has a corresponding Integer class. Figure 26.5 illustrates the use of the int data type, its associated Integer class, and its functions:
Figure 26.5: Integer data type, class, and functions.
The boolean data type has a corresponding Boolean class. Boolean values are "true/false" values. Figure 26.6 illustrates the use of the boolean data type, its associated Boolean class, and its functions:
Figure 26.6: Boolean data type, class, and functions.
The char data type has a corresponding Char class. Char values are single character values. Figure 26.7 illustrates the use of the char data type, its associated Char class, and its functions:
Figure 26.7: Char data type, class, and functions.
Arrays are defined by adding brackets ([]) to the end of the data type or class name in the array definition. Figure 26.8 illustrates the use of arrays:
Figure 26.8: Arrays using the [] characters.