A
A
Alexey24112020-08-31 12:44:30
Java
Alexey2411, 2020-08-31 12:44:30

How are array elements accessed in Java?

I read an article about the differences between arrays in C++ and Java. I realized that in C ++ access to elements occurs by pointer arithmetic, which makes this procedure quite fast ( Which gives an advantage over singly linked lists ). But nowhere did I find a word how this is done in Java. As far as I know, there is no such manipulation of references in Java, and an array is a full-fledged object ( As opposed to a primitive data structure in pluses ). It became interesting how random access to the elements of a Java array is carried out and how fast is this operation?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-08-31
@Aleksey2411

Syntactically the same as in C++. Differences in speed are on the order of nanoseconds, which the virtual machine spends dereferencing a pair of pointers and checking bounds.

J
Jacen11, 2020-08-31
@Jacen11

I don’t know how it is in the pros, but in Java everything is the same, only under the hood and, moreover, it is written on the same pros. In Java, there are arrays, and there are collections that are based on arrays. The array is not an object. A collection, specifically ArrayList, is already an object, and it uses an array at the core of its work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question