If we are able to use .length on an array, does this imply that all arrays are a class with a public final field length?

I don’t quite understand how arrays actually work behind the hood in Java. The fact that I can do something like this:

int[] x = {45, 42, 54};
int y = x.length;

Might imply that x is an object with a public final field length. However, arrays don’t seem to work in the same way as other objects do; thay have different syntax (like [ and ] instead of <>) and I can’t find the source code for an array online (there is an Arrays class, but this is a static class for array helper methods)

How exactly do arrays work? What are they really?

I’ll write this very loosely, I’m no expert on this.

Under the hood, you can think of arrays as consecutive storage lockers in memory (traditionally it’s this way though there are some languages that don’t do this). Having everything near each other makes it simpler to have quick access and operations. The subscript brackets are really using a multiplier on the size of the locker and it’s initial memory location. So for example, if the array is stored in 4 byte chunks, my_array[7] actually means access the information stored in my_array address + (7*4) (where the address is just the location where my_array[0] is stored).

Normally when arrays are declared they are fixed in size. Dynamic arrays have a property where when it’s about to be filled up, it doubles in size. (I think dynamic arrays are called ArrayList in java, it’s definitely vector in c++, and confusingly list in python… although in retrospect list might be a more functional math-centric way to name it but I don’t know :thinking:).

If you’re interested in this kind of thing I heavily recommend the book Computer Organization and Design (link to listing for the book).

1 Like

I do somewhat understand what an array is in the computer itself, I’m just sure not exactly sure how Java specifically represents them. I know that it is technically an object, but is it like a superset or a special type of class or something?

All classes extend Object and the class Object is like a special class, do arrays have the same idea since other collection classes (ArrayList, HashMap, HashSet, etc) use array so it’s some special (or default) type of iterable/collection/class?

And yes, to answer your uncertainty there is an ArrayList in Java, which is just an abstraction of an array that when it fills up, a new array with 50% more size is created and the elements are copied over (and some other useful properties like getters, setters, etc). jdk8/jdk8/jdk: 687fd7c7986d src/share/classes/java/util/ArrayList.java. Note that not all dynamic arrays double their size- Java ArrayLists increase by 50% while Java vectors double their size once they are full

A ‘list’ is confusing like you said- I believe it refers to a dynamic array (and sometimes, like in the case of python but not java, the ability to hold heterogeneous elements). In Java, list is just an interface, and ArrayList, LinkedList, Vector and Stack (Stack extends Vector) implement it so it’s more of a ‘blueprint’

It’s pretty complicated. Arrays extend the Object class. So all methods of the Object class can be called on arrays.

You can write the following and the java compiler will accept it:

Object array = new int[5];

But the various array types are not actually their own classes, they do each have their own corresponding class object.

The closest I could find to an under the hood explanation was here:
https://docs.oracle.com/javase/specs/jls/se11/html/jls-10.html#jls-10.8

1 Like