Primitive Data Types

Java supports eight basic data types known as primitive types as secribed in Table 2-1. The primitive types include a boolean type, a character type, four integer types, and two floating-point types. The four integer types and the two floating-point types differ in the number of bits that represent them and therefore in the range of numbers they can represent. The next section summarizes these primitive data types. In addition to these primitive types, Java supports nonprimitive data types such as classes, interfaces, and arrays. These composite types are known as reference types, which are introduced in Section 2.9 later in this chapter.

Table 2-1. Java primitive data types

Type

Contains

Default

Size

Range

boolean

true or false

false

1 bit

NA

char

Unicode character

\u0000

16 bits

\u0000 to \uFFFF

byte

Signed integer

0

8 bits

-128 to 127

short

Signed integer

0

16 bits

-32768 to 32767

int

Signed integer

0

32 bits

-2147483648 to 2147483647

long

Signed integer

0

64 bits

-9223372036854775808 to 9223372036854775807

float

IEEE 754 floating point

0.0

32 bits

1.4E-45 to 3.4028235E+38

double

IEEE 754 floating point

0.0

64 bits

4.9E-324 to 1.7976931348623157E+308

The boolean Type

The boolean type represents truth values. This type has only two possible values, representing the two boolean states: on or off, yes or no, true or false. Java reserves the words true and false to represent these ...

Get Java in a Nutshell, 5th 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.