Chapter 3. Fundamental Types
Fundamental types include the Java primitive types and their corresponding wrapper classes/reference types. Java 5.0 and beyond provide for automatic conversion between these primitive and reference types through autoboxing and unboxing; see the “Autoboxing and Unboxing” section, later in this chapter. Numeric promotion is applied to primitive types where appropriate.
Primitive Types
There are eight primitive types in Java; each is a reserved keyword. They describe variables that contain single values of the appropriate format and size; see Table 3-1. Primitive types are always the specified precision, regardless of the underlying hardware precisions (e.g., 32- or 64-bit).
Type |
Detail |
Storage |
Range |
|
|
1 bit |
Not applicable |
|
Unicode character |
2 bytes |
\u0000 to \uFFFF |
|
integer |
1 byte |
–128 to 127 |
|
integer |
2 bytes |
–32768 to 32767 |
|
integer |
4 bytes |
–2147483648 to 2147483647 |
|
integer |
8 bytes |
–263 to 263 –1 |
|
floating point |
4 bytes |
1.4e–45 to 3.4e+38 |
|
floating point |
8 bytes |
5e–324 to 1.8e+308 |
Tip
Primitive types byte, short, int, long, float
, and
double
are all signed. Type char
is unsigned.
Literals for Primitive Types
All primitive types, except boolean
, can accept
character, decimal, hexadecimal, octal, and Unicode literal formats, as well as character escape sequences. Where appropriate, the literal value is automatically cast or converted. ...
Get Java Pocket Guide 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.