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


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 TypeWidth (bits)Minimum value MIN_VALUEMaximum value MAX_VALUE
byte8-27 (-128)27-1 (+127)
short16-215 (-32768)215-1 (+32767)
int32-231 (-2147483648)231-1 (+2147483647)
long64-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 TypeWidth (bits)Minimum Unicode valueMaximum Unicode value
char160x0 (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 TypeWidth (bits)Minimum Positive Value MIN_VALUEMaximum Positive Value MAX_VALUE
float321.401298464324817E-45f3.402823476638528860e+38f
double644.94065645841246544e-3241.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 TypeWidthTrue Value LiteralFalse Value Literal
booleannot applicabletruefalse

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 TypeWidth (bits)Minimum Value, Maximum ValueWrapper Class
booleannot applicabletrue, false (no ordering implied)Boolean
byte8-27, 27-1Byte
short16-215, 215-1Short
char160x0, 0xffffCharacter
int32-231, 231-1Integer
long64-263, 263-1Long
float32±1.40129846432481707e-45f, ±3.402823476638528860e+38fFloat
double64'b14.94065645841246544e-324, 'b11.79769313486231570e+308Double

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.216.4.176