Sobes.tech
Middle

Mis on viidetüüpi massiivid?

sobes.tech AI

Vastus AI-lt

Arrays of reference types are data structures that do not store the objects themselves, but references to them in memory. Each element of the array is a pointer to the location of the object in the "heap".

Key features:

  • Store references: The elements of the array contain addresses of objects.
  • Object creation: The objects that the array references are created separately using the new operator. The array itself is also created with new.
  • Default value: For elements of reference types in the array, the default value is null, if no other value is assigned. This means the reference points "nowhere".
  • Dynamic size: Although the size of the array is fixed after creation, the objects it references can be dynamic (e.g., strings or lists).

Example in Java:

// Creating an array of reference type String
String[] names = new String[3];

// Assigning references to String objects
names[0] = new String("Alice");
names[1] = "Bob"; // String literals also create objects

// Default value for uninitialized elements
// names[2] is by default null

// Accessing objects via references
int lengthOfFirstName = names[0].length();