Chapter 3. Fundamental Types

Fundamental types include the Java primitive types and their corresponding wrapper classes/reference types. Java 5.0 and beyond provide for automatic conversion between these primitive and reference types through autoboxing and unboxing; see the “Autoboxing and Unboxing” section, later in this chapter. Numeric promotion is applied to primitive types where appropriate.

Primitive Types

There are eight primitive types in Java; each is a reserved keyword. They describe variables that contain single values of the appropriate format and size; see Table 3-1. Primitive types are always the specified precision, regardless of the underlying hardware precisions (e.g., 32- or 64-bit).

Table 3-1. Primitive types

Type

Detail

Storage

Range

boolean

true or false

1 bit

Not applicable

char

Unicode character

2 bytes

\u0000 to \uFFFF

byte

integer

1 byte

–128 to 127

short

integer

2 bytes

–32768 to 32767

int

integer

4 bytes

–2147483648 to 2147483647

long

integer

8 bytes

–263 to 263 –1

float

floating point

4 bytes

1.4e–45 to 3.4e+38

double

floating point

8 bytes

5e–324 to 1.8e+308

Tip

Primitive types byte, short, int, long, float, and doubleare all signed. Type char is unsigned.

Literals for Primitive Types

All primitive types, except boolean, can accept character, decimal, hexadecimal, octal, and Unicode literal formats, as well as character escape sequences. Where appropriate, the literal value is automatically cast or converted. ...

Get Java Pocket Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.