Skip to main content
Advertisement

5.1 Introduction to Arrays

While programming, situations can arise where you potentially need countless variables to store data such as grades, names, dates, etc. An Array is what we use efficiently to handle these countless variables natively.

1. What is an Array?

It refers to "structuring multiple variables of the same type into a single consolidated bundle". The variable dealing with the array has a form containing several separate 'rooms' within memory; each room is called an element. These elements are stored sequentially adjacent to each other in the memory block.

2. Declaration and Creation of Arrays

Unlike primitive data in Java, arrays are conceptually treated as Objects and handled accordingly via reference variables.

1) Declaring an Array

Space for a reference variable (which acts somewhat like a remote control) to manage the array must be allocated first. You can declare an array by placing square brackets [] either after the type or after the variable name. Most developers favor placing them after the type.

int[] score;   // Declared by placing [] after the type (Recommended)
String[] name;

2) Creating an Array (Allocating Size)

Declaring an array simply secures a place in memory for the reference variable; it doesn't do more than that. To actually build a physical space where elements can be stored, you need to append the memory allocation operator new alongside indicating the required quantity length.

int[] score;         // Declaration
score = new int[5]; // Creating an int array of length 5

// Both steps are habitually performed simultaneously:
int[] score = new int[5];

3. Initialization and Length of Arrays

As soon as an array is created, each segment is initialized to the automatic default value depending on its type explicitly (zero 0 for int, false for boolean, null for String). However, developers possess the flexibility to implement custom values right away.

Array Initialization

If you array values using curly braces {}, its length is determined implicitly by counting those data elements instantly. Here, utilizing the expression new int[] becomes unnecessary.

int[] score = new int[]{ 50, 60, 70, 80, 90 };
// And the above block is abbreviated into primarily handling it like this:
int[] score = { 50, 60, 70, 80, 90 };

Array Length (length)

Should you need to deduce the count of units generated alongside the built array, adding .length beside the array's name acts precisely. Customarily used tightly together with the iteration construct for in Java.

int[] score = new int[5]; // Creating an array of length 5
for (int i = 0; i < score.length; i++) { // Indices are logically valid from 0 down to (length-1)
score[i] = i * 10;
}
System.out.println("The length of the array is: " + score.length);

Crucial Characteristic: An extremely profound attribute is that in Java, once an array's length undergoes creation, the sum of those factors absolutely cannot be modified again. Need it to be lengthier? Initiating a new broader array and duplicating objects is solely necessary.

Advertisement