Java's Primitive Data Types and Reference Data Types
Han
2 min read
Primitive Types
There are a total of 8 primitive types predefined and provided.
As they have default values, they do not have Null.
They are stored in the stack memory, which is the space for storing actual values.
String is an object and not a primitive type.
Data Type | Memory Allocation | Bit | Default Value | Data Range | Numeric Range |
boolean | 1 byte | 1 bit | false | true, false | true, false |
byte | 1 byte | 8 bits | 0 | -128 to 127 | $-2^7 \;to \;2^7 - 1$ |
short | 2 bytes | 16 bits | 0 | -32,768 to 32,767 | $-2^{15} \;to\; 2^{15} - 1$ |
int | 4 bytes | 32 bits | 0 | -2,147,483,648 to 2,147,483,647 | $-2^{31}\; to\; 2^{31} - 1$ |
long | 8 bytes | 64 bits | 0L | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 | $-2^{63}\; to\; 2^{63} - 1$ |
float | 4 bytes | 32 bits | 0.0F | 0.000...0034 to 3400...000 | $3.4 10^{-38}\; to\; 3.4 10^{38}$ |
double | 8 bytes | 64 bits | 0 | 0.00000000...00017 to 17000...000000000 | $1.7 10^{-308} \;to \;1.7 10^{308}$ |
char | 2 bytes | 16 bits | '\u0000' | 0 to 65,535 | $0\; to\; 2^{16} - 1$ |
Reference Types
All types except primitive types are reference types.
Null, which represents an empty object, exists in reference types.
The space for storing the address value of the location where the value is stored is in the heap memory.
There are no syntax errors, but runtime errors occur when using Null for objects or arrays, leading to a NullPointException. Therefore, variables should be assigned values.
Type | Memory Allocation | Default Value |
Array | 4 bytes (address value) | NULL |
Enumeration | 4 bytes (address value) | NULL |
Class | 4 bytes (address value) | NULL |
Interface | 4 bytes (address value) | NULL |
0
Subscribe to my newsletter
Read articles from Han directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by