JAVA arrays

In JAVA, you can store multiple values in arrays.

Arrays in JAVA have a linear storage demand of fixed size. Once created they can only storage a fixed amount of elements.

Arrays are simple and the syntax is easy to understand. Arrays are an in-built part of the JAVA programming language.

Exemplary syntax:

// setting up arrays

String[] names = {“James”, “Sarah”, “Eric”};

int[] numbers = {3,6,9,10,11,1};

// accessing elements in array

System.out.println(names[0])

names[1] = “na”

System.out.println(names[1])

// array .length attribute

System.out.println(names.length)

Since the datatype of the array must be specified at declaration the array will only be able to store that datatype, i.e. all elements must be of that datatype. If you need to store different datatypes in the same array you would e.g. have to specify a custom datatype for this and then store that datatype in the array – or use the Object class i.e. Object datatype. In that case, however, you can only get data out of the array as Object datatype.

Close

Meta