I
I
ipatin2016-04-27 20:25:18
Java
ipatin, 2016-04-27 20:25:18

How are arrays represented in jvm?

How arrays are represented in jvm, how it finds an element by array index, for example somewhere in the middle. It turns out that it still goes through all the elements of the array and finds the right one, or some other mechanism? If so, then what difference does it make for jvm - is it an array or a linked list, why is the operation of finding an element in the middle of an array faster?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nirvimel, 2016-04-27
@nirvimel

Arrays in the JVM are quite real, that is, they occupy a contiguous space in memory as in C / C ++, that is, they are NOT implemented on lists.
An array element access operation has O(1) complexity, and JIT optimizes it down to one machine instruction (on x86), that is, just like accessing a local variable on the stack or a class field.
A single array element is not the same as a single variable or class field, the memory it occupies will not be freed until the entire array is freed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question