This question already has an answer here:
- Where is array's length property defined? 7 answers
- How does array class work in Java? 4 answers
If I use a normal class like List, Vector or something else, I get a size()
function which returns the length of the regarded class but if I use an array of a class or a default data type I get a public member length which returns the current length of the array.
int a[] = new int[3];
a.length; // used without ()
Vector<Integer> v = new Vector<Integer>();
v.length(); // used with ()
Why is that? I mean an array is not an own class isn't it? So if it is no class it can't have a member varaible. I can't get an idea how that is handled in the background (ByteCode). I know that an array in the memory is stored with a pointer to the first element of the array and with the index (i) the memory pointer is shifted to ArrayPointer + i*(size of DataType)
.
Now you can say the computer goes throught all elements of an array and counts all the elements but how can a computer know where an array ends and where the next starts? And from where comes the 'member varaible' from the array where the size is stored?
I mean we use arrays so often but I know so little what happens behind the Java code in the ByteCode. Can you explain to me how that is possible?
Arrays are objects in Java, but they don't correspond to real classes. Effectively, the JVM implicitly creates array classes on the fly, but for performance reasons, they aren't actual classes.
Since they are objects, they can be stored in Object fields and passed around like normal. However, they are treated a bit differently at the bytecode level.
First off, arrays are created with the
newarray
,anewarray
, ormultinewarray
instructions for single dimension primative, single dimension object, and multidimensional arrays respectively. By contrast, regular objects are created with thenew
instruction.Getting and setting elements is done with the
*aload
and*astore
instructions.Also, x.length is not a real field. Instead it gets compiled to the
arraylength
instruction. This can be seen by compiling the following code.results in the following bytecode
Attempting to access the
length
field by manually creating bytecode will result in an exception, because the field doesn't actually exist.results in
Arrays in java have a class, and are therefore objects. Constructing it is done differntly from "normal" classs. More on that maybe in
Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?
Array types actually are classes! As stored in memory, they enclose their basic class information (including the class information of their containing element if they aren't an array of natives), the number of elements they hold, and following that the actual array data.
length
in an array object is afinal
field rather than a method, because arrays never hold a different number of elements.For other kinds of structures, like
Vector
, the number of elements in the object can change from moment to moment, solength()
must be a method. Likewise withString
: even though theString
class is immutable, it inherits thelength()
method fromCharSequence
; some kinds ofCharSequence
can be variable in their length, so it is again implemented as a method.An array is an object you can try this yourself by simply assigning an array to an object value. For example:
If you want to know what an array is on a byte level: the array itself is just pointer to a bit of information on the length of the array, some other information and a pointer to a piece of reserved memory in which you can store the data (sequential memory the size of your array). Accessing the data is a simple matter of looking up the [pointer]+[indexnumber]*[sizeofeachitem] and then you know which piece of memory to look at.
An array is actually a class ,it extends Object like any other class, but its been handled internally by the language for better performance and convenience .As a class it can have fields and methods.