,

Primitive Data Types – Basic Elements, Primitive Data Types, and Operators

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): represents logical values

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 (p. 51).

Primitive data values are not objects, but each primitive data type has a corresponding wrapper class that can be used to represent a primitive value as an object. Wrapper classes are discussed in §8.3, p. 429.

The Integer Types

The integer data types are byte, short, int, and long (Table 2.12). Their values are signed integers represented by two’s complement (p. 34).

Table 2.12 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)

The char Type

The data type char represents characters (Table 2.13). Their values are unsigned integers that denote all of the 65,536 (216) characters in the 16-bit Unicode character set. This set includes letters, digits, and special characters.

Table 2.13 Range of Character Values

Data typeWidth (bits)Minimum Unicode valueMaximum Unicode value
char160x0 (\u0000)0xffff (\uffff)

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.

The integer types and the char type are collectively called integral types.

Related Posts