Lesson 4 – Arrays in Java

In the second lesson, you studied primitive data types in Java. Primitive data types can be used to create more advanced data types. One such data type is an array. Arrays in Java are used to store collection of items of the same datatype. In this lesson, you will studied different array operations.

Creating Arrays

There are two parts to creating arrays in Java. First you have to define an array variable that will store the array. Here is how you can create an array variable:

int [] student_age

In the script above, we create an array variable named “student_age”. To create an array variable, you need to start with the type of the elements that the array will store which in the case of “student_age” array variable is “int”. Next, you have to specify the opening and closing square brackets. The brackets differentiate an array variable from an ordinary variable.  Finally, you have to specify the name of the array. This is only the first part. The “student_age” variable has no physical existence on the memory and points to null at the moment. The second part is to instantiate or create an array object that will be stored in the array variable. To create an array object, you start with the “new” keyword followed by the type of the array and a pair of square brackets. Inside the square brackets, you have to specify the number of elements that the array will contain. The following script stores an array object with 5 items in the “student_age” array.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int [] student_age = new int [10];

}
}

Adding Elements to an Array

To add elements to an array, all you have to do is specify the array name followed by the index inside the square brackets and assign it the value using assignment operator. Remember, indexes in Java arrays start from zero which means that first element will be stored at 0th index. In the following example, the first and second index of the “student_age” array have been assigned values 12 and 5 respectively. Next, the values have been printed on the console.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int [] student_age = new int [10];
student_age[0] = 12;
student_age[1] = 5;

System.out.println(student_age[0]);
System.out.println(student_age[1]);

}
}

Updating an Array

You can update an array by overtiring existing value in any array index. For instance, the following script updates the second index of the array.

public class MainClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

int [] student_age = new int [10];
student_age[1] = 5;
System.out.println("Value before update " + student_age[1]);
student_age[1] = 10;
System.out.println("Value after update " + student_age[1]);

}
}

The output of the above script is as follows:

Value before update 5
Value after update 10

What’s Next?

In this lesson you saw how to create arrays in Java and how to assign and update array values in Java arrays. In the next lesson, you will see conditional statements that are used for decision making in Java.