Sobes.tech
Junior — Middle

Kuidas eristada programmeerimiskeeles põhiliigid viiteobjektidest?

sobes.tech AI

Vastus AI-lt

In programming languages, basic data types (primitives) and reference objects differ in how data is stored and transmitted.

  • Basic data types (e.g., int, float, boolean in Java/Android) are stored on the stack and contain the value directly. When passing a variable, the value itself is copied.
  • Reference objects (e.g., class objects) are stored on the heap, and the variable holds a reference (pointer) to the object. When passing, only the reference is copied, not the object itself.

Example:

int a = 5;
int b = a; // copies the value 5
b = 10;
// a still equals 5

String s1 = new String("hello");
String s2 = s1; // copies the reference to the object
s2 = "world";
// s1 still equals "hello" (String in Java is immutable)

Thus, basic types store data directly, while reference objects do so through a pointer to the object in memory.