Primitive Data Types in Java
This guide details the characteristics, memory specifications, ranges, and execution behaviors of Java's eight primitive data types.
Introduction
In Java, data types define the classification and constraint bounds of data that can be stored in a variable. Primitive data types are pre-defined by the language specifications and represent basic values. They are not objects and do not have methods.
Characteristics of the Eight Primitive Types
Java defines eight primitive data types:
1. byte
- Category: Integer
- Size: 1 Byte (8 bits)
- Default Value:
0 - Range:
-128to127(\(-2^7\) to \(2^7 - 1\)) - Use Case: Useful for saving memory in large arrays, primarily in raw binary file handling or network streams.
2. short
- Category: Integer
- Size: 2 Bytes (16 bits)
- Default Value:
0 - Range:
-32,768to32,767(\(-2^{15}\) to \(2^{15} - 1\)) - Use Case: Rarely used in modern software; was historically used to save memory in memory-constrained devices.
3. int
- Category: Integer
- Size: 4 Bytes (32 bits)
- Default Value:
0 - Range:
-2,147,483,648to2,147,483,647(\(-2^{31}\) to \(2^{31} - 1\)) - Use Case: The default choice for integer values in loops, counters, and general arithmetic.
4. long
- Category: Integer
- Size: 8 Bytes (64 bits)
- Default Value:
0L - Range: \(-2^{63}\) to \(2^{63} - 1\)
- Use Case: Used when values exceed the limits of
int(e.g., timestamps in milliseconds, high-precision astronomical distance values). - Note: Literals must be suffixed with an
Lorl(uppercaseLis preferred to avoid confusion with the number1).
5. float
- Category: Floating-point
- Size: 4 Bytes (32 bits)
- Default Value:
0.0f - Precision: Single-precision (accurate to about 6-7 decimal digits)
- Use Case: Used to save memory in large arrays of floating-point numbers.
- Note: Literals must be suffixed with an
forF.
6. double
- Category: Floating-point
- Size: 8 Bytes (64 bits)
- Default Value:
0.0d - Precision: Double-precision (accurate to about 15-16 decimal digits)
- Use Case: The default type for decimal values in Java.
7. char
- Category: Character
- Size: 2 Bytes (16 bits unsigned)
- Default Value:
\u0000(null character) - Range:
0to65,535 - Use Case: Stores a single character literal in UTF-16 Unicode format. Enclosed in single quotes.
8. boolean
- Category: Logical
- Size: Virtual machine dependent (usually 1 byte or 1 bit in JVM implementation)
- Default Value:
false - Values: Only
trueorfalse - Use Case: Conditional evaluation flags.
Memory Allocation for Primitives
Because primitives have fixed sizes, they are stored directly on the stack memory frame of the executing thread.
For example, when int x = 10 is executed:
Overflow and Underflow Behaviors
Since primitive variables are represented in memory using fixed binary bit depths (using two's complement for signed numbers), exceeding limits results in wrap-around behaviors.
Overflow
Occurs when an operation causes a numeric type to exceed its maximum limit, wrapping around to the minimum value.
public class OverflowDemo {
public static void main(String[] args) {
int val = Integer.MAX_VALUE; // 2147483647
System.out.println("Max: " + val);
System.out.println("Max + 1: " + (val + 1)); // Outputs -2147483648
}
}
Underflow
Occurs when an operation causes a numeric type to fall below its minimum limit, wrapping around to the maximum value.
public class UnderflowDemo {
public static void main(String[] args) {
int val = Integer.MIN_VALUE; // -2147483648
System.out.println("Min: " + val);
System.out.println("Min - 1: " + (val - 1)); // Outputs 2147483647
}
}
Best Practices
- Use double over float: Unless you are memory-constrained or working on graphics algorithms requiring massive arrays of single-precision decimals, default to
doublefor precision. - Choose the correct data type: Do not use
longif values will easily fit into anint, as it doubles memory usage per variable. - Use uppercase L for long literals: Avoid lowercase
lbecause100llooks like the number1001in many console fonts.
Back to Module Home: Introduction to Java Programming