Fundamentals of Arrays

Learn about array features and how to manipulate them.

In Java, an array is an object. However, we also need the help of primitive data types to declare and initialize an array object.

Primitive and nonprimitive data types

Whenever we create an array object with the help of the new keyword, some memory is allocated.

We’ve added comments in the code below to highlight the relationship between the numerical index and the elements of an array.

Press + to interact
//Primitive data types are not objects created from a class.
//They are special data types built into the language.
//To build an array object, we need help from primitive data types (the only exception is String).
//What features distinguish the array data type from other primitive data types?
//Let's see:
//Array Example One
class Main{
public static void main(String[] args){
//an example of a primitive data type
//a variable is a container that contains a value or a primitive data type
int myAge = 54;
System.out.println("An example of a primitive data type : " + myAge);
//in the case of the array data type, we declare and allocate memory with the new keyword
//the following array container holds a fixed number of values of data type int
//the length of the array is 2
int[] myBasket = new int[2];
myBasket[0] = 2;
myBasket[1] = 3;
//each element can be accessed by its corresponding numerical index
System.out.println("The first element of my basket : " + myBasket[0]);
System.out.println("The second element of my basket : " + myBasket[1]);
}
}

We get the following output when we run the code above:

An example of a primitive data type : 54
The first element of my basket : 2
The second element of my basket : 3

The code example above highlights the difference between the primitive data types and arrays.

  • Line 12: It showcases the myAge variable of the int type, representing a single-value primitive data type.
  • Lines 17–19: In contrast, the myBasket array demonstrates the concept of arrays, capable of storing multiple values of the same data type. By comparing these, we emphasize the contrast between a single-value variable and a multivalue container.

When we declare an array in Java, we usually get help from primitive data types. This sounds logical because we need to tell the compiler what type of array we’re creating. The memory will be allocated according to our declaration. One single exception is the String nonprimitive data type. The following example will show how we can handle that.

Press + to interact
//Array Example Two
class Main{
public static void main(String[] args){
//to declare and create an array of integer type
int[] studentClasses = new int[2];
//to declare an array of String (non primitive data type)
String[] studentNames = new String[4];
//the brackets after the data type indicates it's an array
//an array has two parts: data type and name
//the data type also indicates what type of elements the array will contain
//the next line will give an error of incompatible type
/*
studentNames[0] = 12;
*/
studentNames[0] = "John"; //this is OK with the compiler
System.out.println("The name of the student who has arrived first : " + studentNames[0]);
}
}

We get the following output when we run the code above:

The name of the student who has arrived first : John

Here, we can see a slight difference in the String nonprimitive type from the normal arrays.

...