Arrays in Java I: What is an Array?

An array is a collection of variables. In Java these variables need to be of the same type. So, you can have an array of integers, an array of objects, or even an array of arrays.

Arrays are not a particularly favored data structure in Java and therefore are a popular technique to catch job seekers off guard in an interview.

What is an Array?

If you are coming to Java from another programming language, chances are you have already come across the array data structure. Java arrays are a little bit similar to arrays in C and C++ but are light years away from arrays in PHP, JavaScript, Ruby and Python. Even if you have used arrays in C and C++, you need to unlearn the pointer mechanics and the fact that array name is a pointer.

Java arrays, as indicated earlier are a collection of variables. These variables can be of primitive data type or can be of reference data types. Whatever be the type of variables inside the array, there is always exactly one type of variables in an array. You cannot mix and match. So, you cannot put an integer and a float in the same array.

Why Do We Need Java Arrays?

To answer the question succintly – for dealing with a lot of variables effectively at one go. In fact, this is the sole purpose of any collection.

Suppose, we had the following Strings containing name of your favorite programming site:

String str1 = “Coding”;
String str2 = “Raptor”;
String str3 = “.com”;

The variable str1 refers to a memory area that has value “Coding” stored in it. Similarly, str2 points to an area containing “Raptor” and str3 points to area having the value “.com”. The diagram below shows the memory structure. It depicts how each String variable is pointing to its respective value.

It would be, of course, much nicer if somehow we could reduce the number of variables we had to deal with. It would make passing information to and from methods much simpler. Collections provide this benefit and array happens to be the simplest of collection. We could drop using multiple variables and simply declare an array variable that contains all the information as shown in the code below.

String[] favoriteWebsite = {
“Coding”,
“Raptor”,
“.com”
};

There are a various ways of declaring and using arrays, and we will look into all of those. The above code declares a variable called favoriteWebsite and the type of variable is declared to be String[] which is saying that this variable is of type String array.

The memory layout for the above declaration would look something like this

As the figure above shows we now have one single variable that refers to a memory area containing all the information. The memory is contiguous and it’s easy to navigate from one location to the other because all the values are all placed serially.

This contiguous memory structure makes it faster to navigate from one value to next.  Iterating an array is a O(1) operation. This is the biggest advantage of an array. The main appeal of the arrays lies in their fast iteration.

In the next article we will examine how to declare and initialize an array.

Leave a comment

Your email address will not be published.