Sobes.tech
Intern

Mis vahe on primitiivsete ja viidete andmetüüpide vahel?

sobes.tech AI

Vastus AI-lt

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

Reference types are objects stored on the heap, and the variable contains a reference (address) to the object. Examples: classes, arrays, interfaces.

Main differences:

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

For example:

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

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