Basic Arrays in Java
Tags: java, codenewbie, tutorial, 100daysofcode
Published: 2021-11-10 21:23:12

---
title: Basic Arrays in Java
published: true
description: Basic Arrays in Java
tags: Java, codenewbie, tutorial, 100daysofcode
---
The *array* is one of the fundamental and most useful concepts in programming. Most programming languages let programmers create and use arrays. Java is one of them.

An array is a group of variables that have the same name and data type. To access one of the variables, use the array name and the integer index of the variable. In Java, array indexes start with `0`. Arrays have a fixed length. Java implements arrays as objects.

To declare an array named `arr` that can hold integer values:

```java
int[] arr;
```

In the code above, `arr` is the variable name.  `int[]` is the type, which is an array of integer values. This code only declares the `arr` variable. Here is how to allocate an array object with `5` integer elements:

```java
int[] arr = new int[5];
```

In this code, `new` is a Java keyword that allocates memory for an object and `int[5]` specifies an array of `5` integer values. Since the default value for Java `int` variables is `0`, the array contains five `0` values in positions `0`-`4`.

Here is another way to allocate the same array:

```java
int[] arr = new int[]{0,0,0,0,0};
```

The array object has a `length` property to indicate the size of the array:

```java
int len = arr.length;
```

In this array example, `arr.length` would return `5`.

To access an individual element of an array, supply an index to the array variable. The index is in square brackets `[ ]` after the array name:

```bash
arr[0] = 100;
arr[1] = 200;
arr[2] = 300;
arr[3] = 400;
arr[4] = 500;

System.out.println( arr[3] ); // 400
```

The first five lines assign the values `100`, `200`, `300`, `400`, and `500` to array positions `0`, `1`, `2`, `3`, and `4` respectively. Note that array elements are like integer variables, except with integer indexes.

The last line outputs the array element at position `3`: `400`. As you can see, an array element can be wherever an integer value is allowed.

Since the array indexes form a sequence of integers, arrays work well with loops. For an array of length `5`, it has the indexes `0`-`4`. Here is an example that uses a `for` loop to output each element of our array:

```java
for (int i=0; i < arr.length; i++) {
  System.out.println(arr[i]);
}
```

And here is the output:

```bash
100
200
300
400
500
```

This example uses the loop variable `i` as the index of the array. The loop repeats its statement five times, once for each index of the array.

What if we want to sum all the elements in the array? It's easy. Use a loop:

```java
int sum = 0;
for (int i=0; i < arr.length; i++) {
  sum = sum + arr[i];
}

System.out.println(sum); // 1500
```

Here is the complete program:

```java
public class Example {
  public static void main(String[] args) throws Exception {
    
    int[] arr = new int[]{0,0,0,0,0};
    
    arr[0] = 100;
    arr[1] = 200;
    arr[2] = 300;
    arr[3] = 400;
    arr[4] = 500;

    int sum = 0;
    for (int i=0; i < arr.length; i++) {
      sum = sum + arr[i];
    }

    System.out.println(sum); // 1500
  }
}
```

Arrays are very useful in programming. Think of an array like a group of variables, all with the same name and data type. Use an index to access each element. You can assign a value to an element of the array or access the value of an element, just like plain variables. Use arrays with loops to see the true power of arrays.

Thanks for reading.

Follow me on Twitter [`@realEdwinTorres`](https://twitter.com/realEdwinTorres) for more programming tips. 😀