Array in Java

In this post we’ll talk about one of the programming construct array in Java. Topics covered are how to declare an array, how to create and initialize an array, how to store elements in an array and how to access those elements using array indexes, types of arrays in Java.

Array in Java

Array in Java is a fundamental programming construct that holds a fixed number of values of a single type. For every type there is also a corresponding array type so you can have array of any primitive data type like int, double, char etc. as well as an array of objects like String, BigInteger or any user defined object.

Array elements are stored in contiguous memory locations, in case of primitive types values are stored in those locations, for an array of objects array holds reference to those objects.

Each item in an array is called an element, and each element is accessed by its numerical index. Index numbering begins with 0. If there are n elements in an array, you can access them using indices from 0 to n−1.

Following image shows an array of length 10 and its type is int.

Array in Java

Declaring an array in Java

Syntax for Java array declaration is as follows-

Note that both ways of array declaration are valid but as per Java doc type array-name[] form is discouraged. That’s what Java doc says “However, convention discourages this form; the brackets identify the array type and should appear with the type designation”.

As per syntax an array declaration has two components: the array’s type and the array’s name. An array’s type is written as type[], where type is the data type of the contained elements; the brackets are special symbols indicating that this variable holds an array. An array’s name can be any variable name provided that it follows the rules and conventions for naming variables.

Some examples of declaring array in Java-

Creating and Initializing an array in Java

Note that array declaration does not actually create an array; it simply tells the compiler that this variable will hold an array of the specified type.
For creating an array in Java you use one of the following ways-

  1. Create an array using new operator.
  2. Using array literal where creation and initialization is done in a single line.

Creating array using new operator

Syntax for creating array using new operator is-

here type is the data type of the array elements.

size is the number of elements that can be stored in the array.

array-name – variable that holds reference to the created array.

For example creating an array of length 10 that stores int values.

You can also combine these both steps of declaration and creation into a single step.

Important point-

When an array is created using new operator its elements are automatically initialized to the default value which is as follows-

  • 0 for numeric types.
  • false for boolean.
  • null for object references (array of objects).

Using array literal (combining creation and initialization)

You can also use the short cut syntax to create and initialize an array where curly braces {} are used to provide the values separated by commas.

For example-

Here the length of the array is determined by the number of values provided between braces and separated by commas.

Java array creation examples

1- Following Java example creates an array of integers, initialize it and later access the array values using array indices in a for loop.

Output

Creating array in Java

Array of integer values

2- In this example we’ll see how to create an array of objects. In the code an array of type Employee is created where Employee is a user defined class. This array can store objects of type Employee.

Output

Creating array of objects

Array of objects

ArrayIndexOutOfBoundsException- Run time array index checking

A Java array guarantees that it cannot be accessed outside of its range. Java has strict run-time check for any out of range index. Trying to access array index out of the range, either negative number or positive number, will result in ArrayIndexOutOfBoundsException which is a run-time exception.

Output

As you can see here array of length 5 (index 0 to 4) is created. Trying to access index 5 which is out of range results in ArrayIndexOutOfBoundsException.

Multidimensional arrays in Java

You can also declare multidimensional arrays in Java which are actually array of arrays. To declare a multidimensional array, specify each additional dimension using another set of square brackets.

For example creating a two dimensional array of integers-

Multidimensional array Java example

Output

Reference: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

That’s all for the topic Array in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.