Understanding Data Types – Building Blocks – 1Z0-829 Study Guide

Understanding Data Types

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.

Using Primitive Types

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 Primitive Types

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

KeywordTypeMin valueMax valueDefault valueExample
booleantrue or falsen/an/afalsetrue
byte8-­bit integral value-1281270123
short16-­bit integral value-32,76832,7670123
int32-­bit integral value-2,147,483,6482,147,483,6470123
long64-­bit integral value-263263–10L123L
float32-bit floating-point valuen/an/a0.0f123.45f
double64-bit floating-point valuen/an/a0.0123.456
char16-­bit Unicode value065,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.”