Intern
What is the difference between primitive and reference types?
sobes.tech AI
Answer from AI
Primitive types store the actual value of the variable, while reference types store a reference (address) to an object in memory.
Primitive types:
byteshortintlongfloatdoublebooleanchar
Reference types:
- Classes (
String, user-defined classes, etc.) - Interfaces
- Arrays
Key differences:
| Attribute | Primitive types | Reference types |
|---|---|---|
| Store value | Directly in the variable | In the heap, variable stores a reference |
| Size | Fixed and known | Depends on the object's content |
| Default value | 0, 0.0, false, '\u0000' | null |
| Inheritance | No | Yes |
| Methods | No | Yes |