A real perspective of an Array

Justgiveacar
3 min readFeb 2, 2021

What are Arrays in Java ?

“An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.”

The above paragraph is from Oracle Java Official Document, we can know that an array has features such as:

  • Multiple spaces
  • Unchangeable length
  • Arbitrary type

That is, generally, an array can help us to store several elements with same type (like a bank but only provide service to one specific currency), however, we are not allowed to add one more element to the array if length has already reached its maximum capacity.

That’s pretty much it, let’s take a look at an array in practice.

dataType[] nameOfArray; // preferred way
dataType nameOfArray[]; // works but not preferred way

To declare an array, define the variable type with square brackets:

String[] elements;

We have now declared a variavle that holds an array of strings. To insert value to it, we can use an array literal, place the values in a comma-separated list, inside curly braces:

String[] elements = {'a', 'b', 'c'};

To create an array of integers, you could write:

int[] arr = new int[5];arr[0] = 0;
arr[1] = 0;
...

We can directly create an array with some elements only on one line.

int[] arr = {1, 2, 3};

In order to change an array element:

// elements[index] = new_value;elements[0] = 'new_a';

Multidimensional arrays is an also a type of array which is an array containing one or more arrays. To create a two-dimensional array, add each array within its own set of curly braces:

int[][] multiNums = { {1, 2, 3}, {4, 5, 6} };

If you are interested, I want to talk about the underlying knowledge.

Figure 1 : How arrays are created in memory

Neither the Java Language Specification nor the Java Virtual Specification makes any guarantees about how arrays are implemented internally. All it requires is that array elements are accessed by an int index number having a value from 0 to length-1.

A perfectly conformant JVM could use a HashMap to implement arrays. In that case, the elements would be non-consecutive, scattered around memory, and it would need to record the indexes of elements, to know that they are.

Furthermore, the JVM will allocate the storage for array elements as a flat, contiguous chunk of memory. Locating a particular element is trivial: multiply the fixed memory size of each element by the index of the wanted element and add that to the memory address of the start of the array where index * elementSize + startOfArray. This means that the array storage consists of nothing but raw element values, consecutively, ordered by index. There is no purpose to also storing the index value with each element, because the element’s address in memory implies its index, and vice-versa. The complexity of accessing an element is O(1) since it only needs to calculate the address offset by formula given above.

Thanks for reading my article. If you like it, please give me a small 👏. Appreciate ❤️

--

--