Junior — Middle
Kuidas reageerib süsteem/protsess, kui massiivi elemendi väärtus asendatakse uuega?
sobes.tech AI
Vastus AI-lt
In Java, arrays are fixed-size objects containing elements of a single type. If you replace the value of an array element with a new one, the following happens:
- The reference or value in the array cell at the specified index is simply changed to a new one.
- If it is a primitive type (e.g., int), the value is overwritten.
- If it is an object, the reference to the old object is replaced with a reference to the new object.
The system does not create a new array nor change the size — only the content at the index is modified.
Example:
int[] numbers = {1, 2, 3};
numbers[1] = 10; // now the array is {1, 10, 3}