Sobes.tech
Intern

What is the difference between primitive and reference data types?

sobes.tech AI

Answer from AI

Primitive data types in Android (and Java) are basic types that store values directly in memory. These include int, boolean, char, float, and others. They occupy a fixed amount of memory and do not contain methods.

Reference types are objects stored in the heap, with variables holding a reference (address) to the object. Examples include classes, arrays, interfaces.

Main differences:

  • Storage: primitives are stored directly in the variable, references are addresses of objects.
  • Passing: primitives are passed by value, references are passed by value of the reference (i.e., the address is copied).
  • Mutability: primitives are immutable, objects can be mutable.
  • Memory: primitives occupy less memory, objects occupy more.

For example:

int a = 5; // primitive
String s = "Hello"; // reference type

Understanding the difference is important for memory management and program behavior.

What is the difference between primitive and… - sobes.tech