Primitive Data Types in Java
Primitive data types are the most basic data types built into Java. They are not objects, store simple values directly in memory, and offer the fastest performance of any data representation in the language. Java has exactly 8 primitive data types.
1. byte
An 8-bit signed integer with a range of -128 to 127, mainly used to save memory in large arrays where the range fits.
2. short
A 16-bit signed integer ranging from -32,768 to 32,767, used less commonly but useful for memory-conscious applications.
3. int
A 32-bit signed integer with a range of about -2.1 billion to 2.1 billion, and the default choice for whole numbers in Java.
int age = 25;
4. long
A 64-bit signed integer used when values exceed the range of int, and requires an L suffix when writing literals.
long population = 8000000000L;
5. float
A single-precision 32-bit floating-point number, useful for saving memory when decimal precision isn't critical, and requires an f suffix.
6. double
A double-precision 64-bit floating-point number and the default type for decimal values in Java, offering much greater precision than float.
double price = 199.99;
7. char
A single 16-bit Unicode character, written using single quotes, capable of representing letters, digits, and symbols.
char grade = 'A';
8. boolean
Represents only two possible values, true or false, and is commonly used for conditions and flags.
- Primitive types are stored directly in memory (usually the stack), not as objects
- They have no methods of their own and always hold a default value if not initialized
- Each type has a fixed size in memory, which determines its range of possible values
Master Java with Uncodemy
Hands-on training, live projects, and placement support in our Java Programming Course.