Arrays in Java

Arrays in java programming language is a data structure used to store multiple values of same type in a single variable, instead of using different variables for each value. It is a fixed size sequential collection of data of the same type. The elements of an array are stored in a contiguous memory location.

Key Characteristics of Arrays

  • Fixed Size : Once the size of an array is defined, it cannot be changed during runtime.
  • Same Data Type : All elements in an array must be of the same data type.
  • Zero-Based Indexing : Array indices start from 0.
The first address of the array belongs to the first element and the second address to the second element of the array. Here are some examples, when we should store data in an Array:

  • To store the height of each student of a class.
  • To store first 100 prime numbers
  • To store the salaries of all employees of a company.

Suppose, you want to store the height of 100 students. You can store height of all students by creating 100 different variables for each individual student like student1Height, student2Height, student3Height ... student100Height. This is very tedious, time consuming and difficult to maintain method of storing 100 numbers.

However, Java programming language provides array data structure, which solves the problem of storing 100 values of same data type. We can declare an integer array of length 100 to store the height of all 100 students like(We will learn about array declaration later)

int studentsHeight[100];
Here are the main features of an array in Java
  • All the array elements are stored under one variable name. Same name is used to access any element of an array.

  • Arrays elements can be of basic data types such as int, double, char etc or user-defined datatypes like structures and objects. But an array cannot have elements of different data types. We cannot store five integer and five double values in same array.

  • Arrays are considered as objects in Java.

  • The array index starts from 0. In an array of X elements, first element is at index 0 whereas last element is at index X-1.

  • The elements in the arrays are stored in a contiguous memory location. This makes it easy for the user to find the locations of elements and traverse array using array index.

  • For arrays in java, the memory is allocated dynamically in heap area.

  • We can find the length of an array using it's ‘length’ field.

Here is an example of an integer array containing eight elements. This is an array of odd numbers named "odd". All the elements are integers and homogeneous. Index of an array always starts from zero and goes up to n-1(n is size of array). In this case, as there are eight elements, the index is from 0 to 7.

Java Array Index

Advantages of Arrays in Java

  • Java arrays enable you to access any element randomly with the help of indexes.
  • It is easy to store and manipulate large data using array.

Disadvantages of Arrays in Java

  • The size of an array gets fixed during declaration. We cannot increase or decrease array size later.
  • We cannot store values of different data types in an array. Arrays in java cannot store heterogeneous data.

Declaring Array Variables in Java

In Java, we can declare an array as follows:

data_type[] array_name;
OR 
data_type array_name[];
  • data_type : It represents the data type of the elements to be stored in array. It can be basic data types like int, double, float etc. or objects.
  • array_name : It is a valid java identifier. We will use array_name to access the elements of array.
For Example:
int[] primeNumbers;
int primeNumbers[];

Here, primeNumbers is an array which can store integers values.


How to Initialize an Array in Java

When an array is declared, only a reference of an array is created. Based on the number of elements array is expected to store, we have to allocate memory for the array with the help of a new operator. The size of the array should be of constant integer type without any sign (+ or -).

// declare an array
data_type[] array_name;
// Allocate memory for array
array_name = new data_type[size];
For Example:
int[] primeNumbers;
primeNumbers = new int[100];

We can declare and allocate memory of an array in one single statement.

data_type[] array_name = new data_type[size];
For Example:
int[] primeNumbers = new int[100];

The elements in the array allocated by new operator will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types). Instead of using new operator, you can also initialize an array with values while declaring the array. Following is the syntax of initializing an array with values.

data_type array_name[] = {value1, value2, value3, ...}
For Example:
//Declaration and initialization of array
int[] evenNumbers = {2, 4, 6, 8, 10, 12};

Here, we have created an array named "evenNumbers" and initialized it with the values inside the curly brackets. Here, we have not mentioned the size of the evenNumbers array. The Java compiler will utomatically specifies the size by counting the number of elements in the curly brackets.

We can also initialize individual elements of an array in Java, using the index number. For example,

int[] evenNumbers = new int[6];

// Initialize each element of array 
evenNumbers[0] = 2;
evenNumbers[0] = 4;
evenNumbers[0] = 6;
evenNumbers[0] = 8;
evenNumbers[0] = 10;
evenNumbers[0] = 12;

How to Access Elements of an Array in Java

Arrays in Java is a random access data structure. We can access any element of an array using it's index number. Here is the syntax for accessing elements of an array.

array[index]

Here is a java program to access array elements using index numbers.

public class ArrayElementAccess {
  public static void main(String[] args) {
    // create an array
    int[] evenNumbers = {2, 4, 6, 8, 10, 12};
    int i; // array index

    for (i = 0; i < evenNumbers.length; i++) {
      System.out.println(evenNumbers[i]);
    }
  }
}
Output
2
4
6
8
10
12

In the above example, we are using the index number to access each element of the array starting from index 0 to length-1. We are using for loop to access all the elements of the array.


Conclusion

Arrays are foundational data structures in Java, providing a straightforward way to organize and manipulate collections of elements. Understanding how to declare, initialize, access, and manipulate arrays is crucial for any Java developer. As you continue to work with Java, practice using arrays in various scenarios to solidify your understanding and enhance your programming skills.