We all know that the Java programming language is a purely object language. All entities with which we have to deal with and perform any manipulations are objects (with the exception of primitives), including arrays. That is, if to speak absolutely simple words, then any array is specific object in memory. Java is a strongly typed language. Although the gradation into languages ​​with weak and strong data typing is very conditional, but Java, one way or another, is more relevant to languages ​​with strong typing. This leads us to the fact that all data has its fixed type (or class, if we speak in terms of OOP). Here is the whole snag! I always wanted to know how arrays are described, like some abstract entities, on the physical level. I understand that it is impossible to find a ready-made class in which the structure of arrays would be described, for the simple reason that this entity is one of the fundamental (as well as primivnyh data types) and its implementation is hidden somewhere on the JVM level or in what somewhere else.

Let's look at a trivial example. We know that the size of any array is fixed and is determined at the stage of creating the array itself. Array size information is stored in an integer variable with the same name length. Immediately a question arises regarding this field. Where did it come from? Where can I trace all this internal logic (if I may say so)? We go further. We created an array in memory, while immediately specifying its size. The size of the array corresponds to the number of similar elements that can be stored in this array. And here again the question. By what logic does JVM determine the number of elements we need? More precisely, not quite. It’s clear that we ourselves specify the size of the array, but doesn’t the number of fields for a single data type have to be fixed ?! Is there any code (even pseudocode) that could shed some light on this question?

  • one
    View source codes - Anton Sorokin
  • one
    @Lexoid Look here for a Class Array - Nick_Jv
  • one
    At the JVM level, there are only a few array implementations. Each under a separate type of stored content. If this is a primitive type, then for int is a memory block of 4 * n bytes, if it is a reference type, then it can already be either 4n or 8n, depending on the system capacity. An object is not stored in arrays, only references to objects are stored in an array, and the links have a fixed length. In general, I do not see anything incomprehensible. - Serhii Dikobrazko
  • one
    @Lexoid The size of the object reference depends on the system capacity. In 64 bit they are twice as large. Therefore, in 32x, perchemal int takes up actually 128 bits, and in 64x, it takes 224 battles. The size of the primitives for arrays remains unchanged, but I will not argue here. There its subtleties exist, such as as a boolean variable, it seems that it can store its value in only one bit, but instead uses memory as a full byte variable of 8 bits. - Serhii Dikobrazko
  • one
    slideshare.net/cnbailey/java-code-to-java-heap View this. Useful - Serhii Dikobrazko

1 answer 1

I hope my answer will help you. Also attached links to sources for detailed study.

  • In Java, arrays are objects and are inherited from Object
  • The variables in the array are ordered, the array indexing starts at 0.

When we create an array object with the help of new , memory is allocated in the heap for this case and returns us a link. But how is that? The type of the array is written as type [] , where type is the data type of the contained elements; [] brackets are special characters indicating that this variable contains an array; the number that is passed to new int[5] is the size of our array.

enter image description here

What is a java array in memory?

  • The size of the array is not part of its type, so the brackets are empty int[] array;
  • an array of objects will contain references to these objects, and if it is not yet initialized, then null ( Default values ​​in Java )

You can read more about arrays here: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

To understand more about how to create an array, consider the equivalent entry:

 String[] array = (String[]) Array.newInstance(String.class, 3); 

This entry is equal to this:

 String[] array = new String[3]; 

the method (Class<?> componentType, int length) returns us newArray(componentType, length) , and now the implementation of newArray() is native . And this beautiful and hidden from our eyes method returns us to our array and then we bring to the class [Ljava.lang.String with (String[]) Array.newInstance(String.class, 3) .

References to sources:

At the JVM level, using the example int[] :

  • A new array of integers is created during the execution of the operation newarray int
  • The array size must be on the stack before performing this operation; the operation leaves a link to the array on the stack. Links are loaded and saved using iaload var and iastore var .

It should be said that at the level of JVM, depending on the type, the corresponding commands will be called. Read more: https://www.artima.com/underthehood/objectsP.html

  • if the array reference is at the top of the stack, the arraylength operation will leave the length of the array on top of the stack. And probably just the Array.getLength(array) method returns it to us.

More details:

  • one
    It turns out that the array can be inherited? - Flippy
  • one
    @Flippy it is probably final - Aqua
  • one
    SeeSharp is right, it's marked as final - Nick_Jv
  • one
    @Lexoid The documentation itself says that the Array class provides static methods for dynamically creating and accessing Java arrays. - Nick_Jv pm
  • one
    @Lexiod I assume that we get Object at the output. The JVM knows how this object is built and knows what type it can be converted to or ClassCastException (which can be checked, for example: Object array = (String) Array.newInstance(String.class, 3) , but Object array = (String[]) Array.newInstance(String.class, 3) work without any complaints). I couldn’t find anything on this subject, so excuse me, I can’t say exactly what is happening. - Nick_Jv