Introduction to Primitive Data Types in Java
Primitive data types are the foundation for data processing and storage in Java, as they store simple values efficiently and without the overhead of objects. This article provides an introduction to the basic properties of each primitive data type and explains their memory requirements and value ranges.
Java has eight primitive types: four integer types (byte, short, int, long), two floating-point types (float, double), one character type (char), and one boolean type (boolean).
Unlike objects, which in Java are always managed as references to more complex storage structures, primitive data types such as int, float, double, or boolean are stored directly on the stack or in main memory. This means that values are stored without additional memory overhead, as they are not treated as separate objects with metadata or methods. As a result, primitive data types require less memory and allow for faster access times, as no object-oriented overhead, such as managing references or creating objects in heap memory, is necessary.
Each primitive type in Java is associated with a corresponding wrapper class. These wrapper classes thus allow for object-oriented use of primitive data types and support what is known as autoboxing and unboxing, where Java automatically converts primitive values into their wrapper objects and vice versa. All wrapper classes in Java reside in the java.lang package (and do not need to be imported).
Integer Types
The integer types in Java (byte, short, int, long) store whole numbers. Each integer type occupies a specific amount of memory, making it suitable for different use cases:
Data Type | Memory Size | Value Range | Description | Wrapper Class |
---|---|---|---|---|
byte |
8 bits | -128 bis 127 | Suitable for small integers, saving memory | Byte |
short |
16 bits | -32,768 bis 32,767 | Used for integers when `int` is too large | Short |
int |
32 bits | -231 bis 231 - 1 | Standard data type for integers in Java | Integer |
long |
64 bits | -263 bis 263 - 1 | Suitable for very large integers | Long |
Floating-Point Types
Java provides two floating-point data types, which are ideal for scientific calculations, graphics programming, and applications where decimal and fractional values are needed.
Data Type | Memory Size | Value Range | Description | Wrapper Class |
---|---|---|---|---|
float |
32 bits | ±3.4 x 10-38 bis ±3.4 x 1038 | Used for single-precision floating-point numbers | Float |
double |
64 bits | ±1.7 x 10-308 bis ±1.7 x 10308 | Standard data type for double-precision floats | Double |
Char Type
The char type in Java stores individual Unicode characters with 16 bits (value range from 0 to 65535). This allows it to store any character from the Unicode standard, enabling internationalization and support for multiple languages.
Unicode uses what are called "surrogate pairs" (two char values together) for characters beyond the 16-bit limit: through this extension, Java can support Unicode characters beyond the 16-bit boundary, allowing for complete internationalization and multilingual support.
Data Type | Memory Size | Value Range | Description | Wrapper Class |
---|---|---|---|---|
char |
16 bits | Unicode-Zeichen: 0 bis 65535 | Stores individual Unicode characters | Character |
Boolean Type
The boolean type in Java stores one of two values: true or false, and occupies only 1 bit of memory.
Data Type | Memory Size | Value Range | Description | Wrapper Class |
---|---|---|---|---|
boolean |
1 bit | `true` oder `false` | Stores truth values | Boolean |