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. 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 double are 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. Remember that bits are lost during truncation. The following is a list of primitive assignment examples:

boolean isTitleFight = true; ...

Get Java 7 Pocket Guide, 2nd Edition 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.