Junior — Middle
What is the difference between primitive data types and reference types in Java?
sobes.tech AI
Answer from AI
In Java, primitive data types (such as int, boolean, double) directly store values and occupy a fixed amount of memory. They are not objects and do not have methods.
Reference types (such as class objects, arrays) store references (pointers) to the memory area where the object data is located. They can have methods and properties.
Example:
int a = 5; // primitive type
String s = "hello"; // reference type
When passing primitives to methods, the value is copied, whereas when passing reference types, the reference to the object is copied.