What are arrays?

Arrays are important static data structures that are used to store information in one place. 

Declaring and initializing an array

To create an array you need the data type, eg int, String, object, a variable name, and a size. Because arrays are static, once you create an array you cannot change the size. 

int size = 4; 
int[] name = new int[size]; 
int[] name1 = {1, 2, 3, 4}; 

Arrays are accessed through their indices. In Java (unlike languages like Fortran), indexing starts at 0. The first element is accessed through name[0], the second through name[1] and so on. 

name1[0] = 2 
//Now the array looks like this: [2, 2, 3, 4]

If the array has not been specifically set to a value, it will default to 0 for integers and null for objects. From the previous code segment, the array name would look like this: [0,0,0,0]. 

Arrays have a feature called length, which will give you the size of the array. 

int size = name.length 
System.out.println(size); //Output: 4

These structures are important for storing multiple values in a single variable. There are no issues with memory shortage or overflow because it is static. This is also a drawback as you cannot change the size of the array.

Full course
Next Lesson