Java's Primitive Data Types and Reference Data Types

·

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 TypeMemory AllocationBitDefault ValueData RangeNumeric Range
boolean1 byte1 bitfalsetrue, falsetrue, false
byte1 byte8 bits0-128 to 127$-2^7 \;to \;2^7 - 1$
short2 bytes16 bits0-32,768 to 32,767$-2^{15} \;to\; 2^{15} - 1$
int4 bytes32 bits0-2,147,483,648 to 2,147,483,647$-2^{31}\; to\; 2^{31} - 1$
long8 bytes64 bits0L-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807$-2^{63}\; to\; 2^{63} - 1$
float4 bytes32 bits0.0F0.000...0034 to 3400...000$3.4 10^{-38}\; to\; 3.4 10^{38}$
double8 bytes64 bits00.00000000...00017 to 17000...000000000$1.7 10^{-308} \;to \;1.7 10^{308}$
char2 bytes16 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.

TypeMemory AllocationDefault Value
Array4 bytes (address value)NULL
Enumeration4 bytes (address value)NULL
Class4 bytes (address value)NULL
Interface4 bytes (address value)NULL

Did you find this article valuable?

Support Eunhan's blog by becoming a sponsor. Any amount is appreciated!