Java Arrays in 5 Minutes

Why did the programmer quit his job? Because he didn't get arrays.

JT Earl
5 min readFeb 15, 2021

What is an array

An array is a grouping of the same typed values in computer memory. In Java, the data stored in an array can be a primitive or an object. As a quick refresher, primitive data types in Java are byte, short, int, long, float, double, or character. When storing primitives in an array Java stores these in adjacent blocks of memory. An element is an individual value in an array. The index is the location of an element in the array.

Creating an array

In Java, there are two ways to create an array. The first way is using the new keyword as shown below.

int[] array = new int[10];

The other way to create an array is using a shortcut syntax. The shortcut syntax allows us to instantiate the array with exactly the values we need. Using this syntax, we skip having to do the costly operation of looping through the array to insert an element at each index.

double[] array = {10.2, 15.4, 20.6, 25.8};

If you don’t know the elements that you want to put in your array when you instantiate it go with the new keyword.

It’s important to note that no matter how we create an array we need to know the size we want to allocate to the array. The indexes of the array are zero-indexed. If you have an array length of 10 the first element of this…

--

--

JT Earl

Programmer Since 17. Currently working in front-end and mid-tier programming for a finance company. Check out my Tech blog @ documentobject.com