Skip to main content
Logo image

Section 8.2 Types

In Java, we distinguish primitive types and reference types. The reference types are further categorized into classes and array types. The primitive types are very similar to types we know from C. However, their size and arithmetic is exhaustively defined in Java.
Table 8.2.1. Primitive types in Java
Type Description Value Range Default Value
byte 8-bit integer \(-128\dots 127\) 0
short 16-bit integer \(-32768\dots 32767\) 0
int 32-bit integer \(-2^{31} \dots 2^{31}-1\) 0
long 64-bit integer \(-2^{63} \dots 2^{63}-1\) 0L
char 16-bit Unicode character '\u0000' ... '\uFFFF' '\u0000'
float 32-bit IEEE 754 Floating Point 0.0f
double 64-bit IEEE 754 Floating Point 0.0
boolean truth values false, true false
The primitive types can be implicitly converted according to the order in Figure 8.2.2. This order results from the subset relation of the value ranges given in Table 8.2.1. char is the only unsigned data type in java and therefore neither subset nor superset of short and byte. The type boolean cannot be converted implicitly to another type and no type is implicitly converted to boolean.
Figure 8.2.2. Implicit type conversion between primitive types
For other conversions (excluding conversions to boolean), we can use explicit type casts: The expression (T)e converts the subexpression e to the type T. Such explicit casts can lose precision. For instance, (byte)1024 is equal to 0.

Example 8.2.3. Implicit and Explicit Type Conversions.

short s = 1;
byte  b = 2;
int   i = 3;
i = s; // ok
b = s; // not ok, as short is not implicitly
       // convertible to byte
b = (byte)s; // ok, as it is an explicit cast