Java applications contain two types of data: primitive types and reference types. In this sec-tion, we discuss the differences between a primitive type and a reference type.
Java has eight built-in data types, referred to as the Java primitive types. These eight data types represent the building blocks for Java objects, because all Java objects are just a com-plex collection of these primitive data types. That said, a primitive is not an object in Java, nor does it represent an object. A primitive is just a single value in memory, such as a number or character.
The exam assumes you are well versed in the eight primitive data types, their relative sizes, and what can be stored in them. Table 1.6 shows the Java primitive types together with their size in bits and the range of values that each holds.
TABLE 1. 6 Primitive types
Keyword | Type | Min value | Max value | Default value | Example |
boolean | true or false | n/a | n/a | false | true |
byte | 8-bit integral value | -128 | 127 | 0 | 123 |
short | 16-bit integral value | -32,768 | 32,767 | 0 | 123 |
int | 32-bit integral value | -2,147,483,648 | 2,147,483,647 | 0 | 123 |
long | 64-bit integral value | -263 | 263–1 | 0L | 123L |
float | 32-bit floating-point value | n/a | n/a | 0.0f | 123.45f |
double | 64-bit floating-point value | n/a | n/a | 0.0 | 123.456 |
char | 16-bit Unicode value | 0 | 65,535 | \u0000 | ‘a’ |
Is String a Primitive?
No, it is not. That said, String is often mistaken for a ninth primitive because Java includes built-in support for String literals and operators. You learn more about String in
Chapter 4, but for now, just remember it’s an object, not a primitive.
There’s a lot of information in Table 1.6. Let’s look at some key points:
The byte, short, int, and long types are used for integer values without dec-imal points.
Each numeric type uses twice as many bits as the smaller similar type. For example, short uses twice as many bits as byte does.
All of the numeric types are signed and reserve one of their bits to cover a negative range. For example, instead of byte covering 0 to 255 (or even 1 to 256) it actually covers -128 to 127.
A float requires the letter f or F following the number so Java knows it is a float.
Without an f or F, Java interprets a decimal value as a double.
A long requires the letter l or L following the number so Java knows it is a long. Without an l or L, Java interprets a number without a decimal point as an int in most scenarios.
You won’t be asked about the exact sizes of these types, although you should have a gen-eral idea of the size of smaller types like byte and short. A common question among newer Java developers is, what is the bit size of boolean? The answer is, it is not specified and is dependent on the JVM where the code is being executed.
Signed and Unsigned: short and char
For the exam, you should be aware that short and char are closely related, as both are stored as integral types with the same 16-bit length. The primary difference is that short is signed, which means it splits its range across the positive and negative integers. Alterna-tively, char is unsigned, which means its range is strictly positive, including 0.
Often, short and char values can be cast to one another because the underlying data size is the same. You learn more about casting in Chapter 2, “Operators.”