Java #6 - Array (Part-1)

Java #6 - Array (Part-1)

1. What is an array? 2. One-dimensional array 3. Where is the array saved in memory?

What is an array?

A variable can store only one value. Consider a program where we have to save the grade of 50 students, we need to have 50 variables which is not very convenient to create those many variables and use them. In such scenarios, using an array makes more sense.

An array is a container that can hold a fixed number of values/items of a single type (primitive data type or class). These items are called elements of the array. These elements do not have a name, to access these elements we can use array name and index. In other words, we can say, an array is a group of like-typed variables that are referred to by a common name.

In Java, arrays are objects and are dynamically created.

One-dimensional Arrays

A one-dimensional array is a list of like-typed variables.

Array Declaration:

type var-name[ ];

Here, the type declares the element type (also called the base type) of the array. The element type determines the data type of each element that comprises the array. Thus, the element type for the array determines what type of data the array will hold.

For example, the following declares an array named month_days with the type “array of int”:

int month_days[];

Array Creation:

To link month_days with an actual, physical array of integers, you must allocate one using new and assign it to month_days. new is a special operator that allocates memory.

The general form of new as it applies to one-dimensional arrays appears as follows:

array-var = new type [size];
month_days = new int[12];

Here, type specifies the type of data being allocated, size determines the number of elements in the array, and array-var is the array variable linked to the array. Once an array object is created, its length never changes.

The array's length is available as a final instance variable length.

Let’s review: Obtaining an array is a two-step process.

  • First, you must declare a variable of the desired array type.
  • Second, you must allocate the memory that will hold the array, using new, and assign it to the array variable. In Java, all arrays are dynamically allocated.

The elements in the array allocated by new will automatically be initialized to 0 (for numeric types), false (for boolean) or null (for reference types).

Array Access

Array elements can be accessed using an index that starts from 0 and ends with n-1 if the size of the array is n.

Syntax - To access an array using index

array-var[index] = value;
monthDays[2] = 31; // this will set 2nd index to value 31

Array Initialization

Arrays can be initialized when they are declared. An array initializer is a list of comma-separated expressions surrounded by curly braces.

int[] monthDays = {31,28,31,30,31,30,31,31,30,31,30,31}
// Note that a new keyword is not needed here.

Example

public class Arrays {
    public static void main(String[] args) {

        //Array declaration and allocating memory using new, at this point values assigned to array component is 0
        int[] monthDays = new int[12];

        // Print all element in array
        for (int i = 0; i < monthDays.length; i++){
            System.out.print(monthDays[i] + " ");
        }
        System.out.println();
        //Array class name - array of int -> [I (Array are object in Java)
        System.out.println(monthDays.getClass().getName());

        //Assign 31 to 2nd index
        monthDays[2] = 31;

        // Print all element in array
        for (int i = 0; i < monthDays.length; i++){
            System.out.print(monthDays[i] + " ");
        }
    }
}

Output

image-2.png

Where array is saved in memory?

As we have seen above, the array is an object, so it will be saved in heap memory which is dynamically allocated whereas an array reference will be saved in stack memory.

image.png

Index starts from zero because the array reference variable points to a heap memory location so the first element in the array can be accessed directly that's why the index for 1st element is 0.

Did you find this article valuable?

Support SUBODH SINGH by becoming a sponsor. Any amount is appreciated!