The primitive data types supported by the Java Virtual Machine are the numeric types, the boolean type (§2.3.4), and the returnAddress type (§2.3.3).
The numeric types consist of the integral types (§2.3.1) and the floating-point types (§2.3.2).
The integral types are:
byte, whose values are 8-bit signed two's-complement integers, and whose default value is zeroshort, whose values are 16-bit signed two's-complement integers, and whose default value is zeroint, whose values are 32-bit signed two's-complement integers, and whose default value is zerolong, whose values are 64-bit signed two's-complement integers, and whose default value is zerochar, whose values are 16-bit unsigned integers representing Unicode code points in the Basic Multilingual Plane, encoded with UTF-16, and whose default value is the null code point ('\u0000')
The floating-point types are:
float, whose values exactly correspond to the values representable in the 32-bit IEEE 754 binary32 format, and whose default value is positive zerodouble, whose values exactly correspond to the values of the 64-bit IEEE 754 binary64 format, and whose default value is positive zero
The values of the boolean type encode the truth values true and false, and the default value is false.
The First Edition of The Java® Virtual Machine Specification did not consider boolean to be a Java Virtual Machine type. However, boolean values do have limited support in the Java Virtual Machine. The Second Edition of The Java® Virtual Machine Specification clarified the issue by treating boolean as a type.
The values of the returnAddress type are pointers to the opcodes of Java Virtual Machine instructions. Of the primitive types, only the returnAddress type is not directly associated with a Java programming language type.
2.3.1. Integral Types and Values
The values of the integral types of the Java Virtual Machine are:
- For
byte, from -128 to 127 (-2^7 to 2^7 - 1), inclusive - For
short, from -32768 to 32767 (-2^15 to 2^15 - 1), inclusive - For
int, from -2147483648 to 2147483647 (-2^31 to 2^31 - 1), inclusive - For
long, from -9223372036854775808 to 9223372036854775807 (-2^63 to 2^63 - 1), inclusive - For
char, from 0 to 65535 inclusive
2.3.2. Floating-Point Types and Values
The floating-point types are float and double, which are conceptually associated with the 32-bit binary32 and 64-bit binary64 floating-point formats for IEEE 754 values and operations, as specified in the IEEE 754 Standard (JLS §1.7).
In Java SE 15 and later, the Java Virtual Machine uses the 2019 version of the IEEE 754 Standard. Prior to Java SE 15, the Java Virtual Machine used the 1985 version of the IEEE 754 Standard, where the binary32 format was known as the single format and the binary64 format was known as the double format.
IEEE 754 includes not only positive and negative numbers that consist of a sign and magnitude, but also positive and negative zeros, positive and negative infinities, and special Not-a-Number values (hereafter abbreviated NaN). A NaN value is used to represent the result of certain invalid operations such as dividing zero by zero. NaN constants of both float and double type are predefined as Float.NaN and Double.NaN.
The finite nonzero values of a floating-point type can all be expressed in the form s ⋅ m ⋅ 2^{(e - N + 1)}, where:
- s is +1 or -1,
- m is a positive integer less than 2^N,
- e is an integer between E_{min} = -(2^{K-1}-2) and E_{max} = 2^{K-1}-1, inclusive, and
Nand K are parameters that depend on the type.
Some values can be represented in this form in more than one way. For example, supposing that a value v of a floating-point type might be represented in this form using certain values for s, m, and e, then if it happened that m were even and e were less than 2^{K-1}, one could halve m and increase e by 1 to produce a second representation for the same value v.
A representation in this form is called normalized if m ≥ 2^{N-1}; otherwise the representation is said to be subnormal. If a value of a floating-point type cannot be represented in such a way that m ≥ 2^{N-1}, then the value is said to be a subnormal value, because its magnitude is below the magnitude of the smallest normalized value.
The constraints on the parameters N and K (and on the derived parameters E_{min} and E_{max}) for float and double are summarized in Table 2.3.2-A.
Table 2.3.2-A. Floating-point parameters
| Parameter | float |
double |
|---|---|---|
N |
24 | 53 |
| K | 8 | 11 |
| E_{max} | +127 | +1023 |
| E_{min} | -126 | -1022 |
Except for NaN, floating-point values are ordered. When arranged from smallest to largest, they are negative infinity, negative finite nonzero values, positive and negative zero, positive finite nonzero values, and positive infinity.
IEEE 754 allows multiple distinct NaN values for each of its binary32 and binary64 floating-point formats. However, the Java SE Platform generally treats NaN values of a given floating-point type as though collapsed into a single canonical value, and hence this specification normally refers to an arbitrary NaN as though to a canonical value.
Under IEEE 754, a floating-point operation with non-NaN arguments may generate a NaN result. IEEE 754 specifies a set of NaN bit patterns, but does not mandate which particular NaN bit pattern is used to represent a NaN result; this is left to the hardware architecture. A programmer can create NaNs with different bit patterns to encode, for example, retrospective diagnostic information. These NaN values can be created with the Float.intBitsToFloat and Double.longBitsToDouble methods for float and double, respectively. Conversely, to inspect the bit patterns of NaN values, the Float.floatToRawIntBits and Double.doubleToRawLongBits methods can be used for float and double, respectively.
Positive zero and negative zero compare equal, but there are other operations that can distinguish them; for example, dividing 1.0 by 0.0 produces positive infinity, but dividing 1.0 by -0.0 produces negative infinity.
NaN is unordered, so numerical comparisons and tests for numerical equality have the value false if either or both of their operands are NaN. In particular, a test for numerical equality of a value against itself has the value false if and only if the value is NaN. A test for numerical inequality has the value true if either operand is NaN.
2.3.3. The returnAddress Type and Values
The returnAddress type is used by the Java Virtual Machine's jsr, ret, and jsr_w instructions (§jsr, §ret, §jsr_w). The values of the returnAddress type are pointers to the opcodes of Java Virtual Machine instructions. Unlike the numeric primitive types, the returnAddress type does not correspond to any Java programming language type and cannot be modified by the running program.
2.3.4. The boolean Type
Although the Java Virtual Machine defines a boolean type, it only provides very limited support for it. There are no Java Virtual Machine instructions solely dedicated to operations on boolean values. Instead, expressions in the Java programming language that operate on boolean values are compiled to use values of the Java Virtual Machine int data type.
The Java Virtual Machine does directly support boolean arrays. Its newarray instruction (§newarray) enables creation of boolean arrays. Arrays of type boolean are accessed and modified using the byte array instructions baload and bastore (§baload, §bastore).
In Oracle’s Java Virtual Machine implementation, boolean arrays in the Java programming language are encoded as Java Virtual Machine byte arrays, using 8 bits per boolean element.
The Java Virtual Machine encodes boolean array components using 1 to represent true and 0 to represent false. Where Java programming language boolean values are mapped by compilers to values of Java Virtual Machine type int, the compilers must use the same encoding.