# Arrays in Java

Arrays in Java work very similar to the "original" arrays we know from system-near programming languages like C and C++. Therefore, arrays are fixed in size and the size must be defined during declaration.

## Declaring and Initializing an array​

Only declaring array in Java is possible in two ways. Both syntaxes are legitimate.

String people[] = new String[3];String[] people = new String[3];

The examples create empty arrays with the size of 3. Instead, we can also assign the values directly:

String people[] = { "Max", "Anna", "Tom" };

As Java can infer the number of elements, we don't need to provide a size.

A value from an array can be returned with the syntax we know from most programming languages

String[] people = { "Anna", "Max" };System.out.println(people[0]); // AnnaSystem.out.println(people[1]); // Max

## Setting and overwriting array values​

String people[] = new String[3];people[0] = "John";people[1] = "Tom";people[2] = "Anna";

## Looping over arrays​

With a for-loop:

String people[] = { "Max", "Anna", "Tom" };for (int i = 0; i < people.length; i++) {  System.out.println(people[i]);}

Or, with a for-each:

String people[] = { "Max", "Anna", "Tom" };for (String person : people) {  System.out.println(person);}

## Java array methods​

Here is an overview, of the most important methods