We can determine the length of an ArrayList
using its public method size()
, like
ArrayList arr = new ArrayL
To answer it as it-is, where is this length property of array defined? In a special Object header
.
Easy to see via JOL
int [] ints = new int[23];
System.out.println(ClassLayout.parseInstance(ints).toPrintable());
One of the lines from this output is going to be:
OFFSET SIZE TYPE DESCRIPTION
16 4 (object header) 17 00 00 00 (00010111 00000000 00000000 00000000) (23)
Usually Objects have two headers (mark and klass), arrays have one more that always occupy 4 bytes
in length, as size
is an int
.