The Java language supports two distinct type categories: primitive and reference. (Arrays are a special case of reference data type and have unique features that makes it easy to think of them as being their own distinct type category.) Any variables or constants you use in your programs will belong to one of these categories. It’s important to know how the types in each category behave. (The concepts of variables and constants are discussed in detail later in this chapter.)
Primitive type names begin with lowercase letters and DO NOT require memory to be dynamically allocated with the new operator. Table 6-3 lists the primitive data types along with their value ranges.
Primitive Type | Size in Bytes | Default Value | Value Range |
---|---|---|---|
boolean | 1 Bit | false | true or false |
char | 2 | \u0000 | \u0000 to \uFFFF |
byte | 1 | 0 | -128 to 127 |
short | 2 | 0 | -32768 to 32767 |
int | 4 | 0 | -2147483648 to 2147483647 |
long | 8 | 0 | -9223372036854775808 to 9223372036854775807 |
float | 4 | 0.0 | ± 1.4e-48 to ± 3.4028235e+38 |
double | 8 | 0.0 | ± 4.9e-324 to ± 1.7976931348623157e+308 |
Anything not a primitive data type is a reference data type. Reference data types include all the classes of the Java platform and any classes you create as part of your programs. Generally speaking, reference data types begin with capital letters, but you could define classes that begin with lowercase letters if you set your mind to it.
Before using a reference-type object you must first allocate memory space for it using the new operator. The exception to this rule is the String class which holds a special place among Java platform reference types because of the shortcut method you can use to assign values to String objects.
An array is a data structure that’s used to store a collection of homogeneous primitive data type values or references to objects in a contiguous memory allocation. (In other words, an array holds like-sized items arranged one after the other!)
Arrays are reference types in the Java language. This gives them special powers when compared to arrays in programming languages like C or C++. Because arrays are treated like first-class objects they provide additional functionality you will find extremely helpful. Arrays are covered in detail in chapter 8.