What is an array in the Java programming language?


We all know that the Java programming language is a purely object-oriented language. All entities that we have to deal with and perform any manipulations are objects (with the exception of primitives), including arrays. That is, to put it quite simply, any array is a 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 related to languages with strong typing. This leads us to the fact that all data has its own fixed type (or class, in OOP terms). Here's the rub! I've always wanted to know how arrays are described, as some abstract entities, at the physical level. I understand that it is impossible to find a ready-made class that would describe the structure of arrays, for the simple reason that this entity is one of the fundamental (on par with primitive data types) and its implementation is hidden somewhere at the JVM level or in some other place.

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. Information about the size of the array is stored in an integer variable with the same name length. Immediately there is a question about this field. Where did it come from? Where can I trace all this internal information? logic (if I may say so)? Go ahead. We created an array in memory, and immediately specified its size. The size of the array corresponds to the number of elements of the same type that can be stored in this array. And here again is the question. By what logic does the JVM determine the number of elements we need? More precisely, not quite so. It is clear that we ourselves specify the size of the array, but shouldn't the number of fields for a single data type be fixed?! Is there some code (even pseudo-code) that could shed some light on this issue.

Author: Kromster, 2018-08-31

1 answers

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

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

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

enter a description of the image here

What is a Java array in memory?

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

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

To understand more about how an array is created, consider the equivalent entry:

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

This entry is equal to this:

String[] array = new String[3];

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

References to sources:

At the JVM level, for example int[]:

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

It is worth saying that at the JVM level, 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 array length on top of the stack. And probably just the method returns it to us Array.getLength(array)

More detailed:

 3
Author: Nick_Jv, 2018-09-01 02:35:37