-1

I am curious. is java using Pointer like C or C++ to access array by index? when use C languange, x[a] can be converted to *(x + a). What about Java languange? its same with c and c++ or it just use Sequential search to access the element?

Angga Arya S
  • 137
  • 1
  • 11
  • Java is not C, and arrays can only be access using `array[index]` syntax. Also unlike C, you get an error at runtime of `index` exceeds the array size. – Andreas Feb 20 '20 at 14:33
  • In the sense that arrays are a wrappers around contiguous blocks of memory whose entries you access by offsetting the start point, you could regard them as something like pointers under the hood. – khelwood Feb 20 '20 at 14:35
  • Technically, I believe that is an implementation detail of the JVM, it is not a requirement of the Java language, but likely all reasonable JVM implementations use the equivalent of pointer access internally. – Mark Rotteveel Feb 20 '20 at 16:05

2 Answers2

2

If it is an array of objects, it's essentially an array of pointers that reference those objects, this is not the case for primitive values however. Unlike c++, you cannot do pointer arithmetic in Java.

rhowell
  • 1,165
  • 8
  • 21
1

OK, i found an answer from this https://softwareengineering.stackexchange.com/a/105919

In Java, plain pointer arithmetics (referencing and dereferencing) don't exist anymore. However pointers exist. They call them references, but it doesn't change what it is. And array access still is exactly the same thing: Look at the address, add the index and use that memory location. However in Java, it will check whether or not that index is within the bounds of the array you originally allocated. If not, it will throw an exception.

Angga Arya S
  • 137
  • 1
  • 11