2.2 Primitive Data Types

Figure 2.1 gives an overview of the primitive data types in Java.

Figure 2.1. Primitive Data Types in Java

graphics/02fig01.gif

Primitive data types in Java can be divided into three main categories:

  • Integral types? represent signed integers (byte, short, int, long) and unsigned character values (char)

  • Floating-point types (float, double)? represent fractional signed numbers

  • Boolean type (boolean)? represent logical values

Primitive data values are not objects. Each primitive data type defines the range of values in the data type, and operations on these values are defined by special operators in the language (see Chapter 3).

Each primitive data type also has a corresponding wrapper class that can be used to represent a primitive value as an object. Wrapper classes are discussed in Section 10.3.

Integer Types

Table 2.9. Range of Integer Values

Data Type

Width (bits)

Minimum value MIN_VALUE

Maximum value MAX_VALUE

byte

8

-27 (-128)

27-1 (+127)

short

16

-215 (-32768)

215-1 (+32767)

int

32

-231 (-2147483648)

231-1 (+2147483647)

long

64

-263 (-9223372036854775808L)

263-1 (+9223372036854775807L)

Integer data types are byte, short, int, and long (see Table 2.9). Their values are signed integers represented by 2's complement (see Section G.4, p. 598).

Character Type

Table 2.10. Range of Character Values

Data Type

Width (bits)

Minimum Unicode value

Maximum Unicode value

char

16

0x0 (\u0000)

0xffff (\uffff)

Characters are represented by the data type char (see Table 2.10). Their values are unsigned integers that denote all the 65536 (216) characters in the 16-bit Unicode character set. This set includes letters, digits, and special characters.

The first 128 characters of the Unicode set are the same as the 128 characters of the 7-bit ASCII character set, and the first 256 characters of the Unicode set correspond to the 256 characters of the 8-bit ISO Latin-1 character set.

Floating-point Types

Table 2.11. Range of Floating-point Values

Data Type

Width (bits)

Minimum Positive Value MIN_VALUE

Maximum Positive Value MAX_VALUE

float

32

1.401298464324817E-45f

3.402823476638528860e+38f

double

64

4.94065645841246544e-324

1.79769313486231570e+308

Floating-point numbers are represented by the float and double data types.

Floating-point numbers conform to the IEEE 754-1985 binary floating-point standard. Table 2.11 shows the range of values for positive floating-point numbers, but these apply equally to negative floating-point numbers with the '-' sign as prefix. Zero can be either 0.0 or -0.0.

Since the size for representation is finite, certain floating-point numbers can only be represented as approximations. For example, the value of the expression (1.0/3.0) is represented as an approximation due to the finite number of bits used.

Boolean Type

Table 2.12. Boolean Values

Data Type

Width

True Value Literal

False Value Literal

boolean

not applicable

true

false

The data type boolean represents the two logical values denoted by the literals true and false (see Table 2.12).

Boolean values are produced by all relational (see Section 3.9), conditional (see Section 3.12) and boolean logical operators (see Section 3.11), and are primarily used to govern the flow of control during program execution.

Table 2.13 summarizes the pertinent facts about the primitive data types: their width or size, which indicates the number of the bits required to store a primitive value; their range (of legal values), which is specified by the minimum and the maximum values permissible; and the name of the corresponding wrapper class.

Table 2.13. Summary of Primitive Data Types

Data Type

Width (bits)

Minimum Value, Maximum Value

Wrapper Class

boolean

not applicable

true, false (no ordering implied)

Boolean

byte

8

-27, 27-1

Byte

short

16

-215, 215-1

Short

char

16

0x0, 0xffff

Character

int

32

-231, 231-1

Integer

long

64

-263, 263-1

Long

float

32

±1.40129846432481707e-45f, ±3.402823476638528860e+38f

Float

double

64

\'b14.94065645841246544e-324, \'b11.79769313486231570e+308

Double