Junior
What is the difference between primitive and reference data types?
sobes.tech AI
Answer from AI
Primitive data types store the value directly, whereas reference data types store a reference (memory address) to an object.
Primitive types:
- Always have a value (cannot be
null). - Occupy a fixed amount of memory.
- Are passed by value.
- Examples:
byte,short,int,long,float,double,char,boolean.
Reference types:
- Can accept the value
null. - Occupy different amounts of memory depending on the object.
- Are passed by reference.
- Examples: classes (
String,Object, arrays), interfaces.
Comparison:
| Characteristic | Primitive types | Reference types |
|---|---|---|
| Data storage | The value itself | Reference to an object in memory |
| Null value | Cannot be null |
Can be null |
| Memory size | Fixed | Variable |
| Passed to methods | By value | By reference |