Arrays store multiple values of the same type in a single variable

An array in Java is a container that holds a fixed number of items, all of the same data type. Instead of creating ten separate variables to hold ten numbers, you create one array that holds all ten. Arrays make your code shorter and easier to work with when you need to store related data.

Java requires you to declare the array type, decide its size, and then fill it with values. The process is straightforward once you understand the syntax — the square brackets [] tell Java you are working with an array rather than a single value.

Key Takeaways

  • Declare an array by writing the data type, square brackets, and a variable name: int[] numbers;
  • Create the array in memory using the new keyword and specify how many items it will hold: int[] numbers = new int[5];
  • Fill array positions using the index number in square brackets, starting from 0: numbers[0] = 10;
  • You can declare, create, and fill an array in one line: int[] numbers = {10, 20, 30, 40, 50};

Declare and create an array in two steps

The most common way to build an array is to declare it first, then create it. Declaration tells Java what type of data the array will hold and gives it a name. Creation actually builds the array in your computer's memory and sets its size.

To declare an array, write the data type, then square brackets, then the variable name:

int[] numbers;

This line says "I am going to create an array called numbers that will hold integers." But the array does not exist in memory yet. To actually create it, use the new keyword and specify the size:

int[] numbers = new int[5];

This creates an array that can hold exactly 5 integers. Java automatically fills each position with a default value — 0 for integers, false for booleans, and null for text and objects. You can now store values in positions 0 through 4.

Declare and create an array in one line using initialization

If you already know what values you want to store, you can skip the new keyword and use initialization. List the values inside curly braces, and Java figures out the array size automatically:

int[] numbers = {10, 20, 30, 40, 50};

This creates an array of 5 integers and fills each position with the value you provided. The first value (10) goes in position 0, the second value (20) goes in position 1, and so on. This method is faster when you know your data ahead of time.

You can use initialization with any data type. For text (called String in Java), write:

String[] names = {"Alice", "Bob", "Charlie"};

Access and change values using the index number

Each position in an array has a number called an index, starting from 0. To read or change a value, write the array name followed by the index in square brackets.

If you have an array called numbers with 5 positions, the indices are 0, 1, 2, 3, and 4. To store a value in the first position, use index 0:

numbers[0] = 10;

To read the value back out, use the same syntax:

int firstNumber = numbers[0];

This copies the value from position 0 into a new variable called firstNumber. If you try to access an index that does not exist — for example, numbers[5] in a 5-item array — Java will crash with an error called ArrayIndexOutOfBoundsException.

Loop through an array to work with all values at once

When you have many items in an array, you usually do not want to access each one by typing its index manually. Instead, use a loop — a block of code that repeats automatically. The most common loop for arrays is the for loop:

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

This loop starts at index 0 (i = 0), repeats as long as i is less than the array size (numbers.length), and adds 1 to i each time (i++). Inside the loop, it prints each value. The variable numbers.length automatically tells you how many items are in the array, so you do not have to count them yourself.

Java also offers a simpler for-each loop when you just need to read values without worrying about the index:

for (int number : numbers) { System.out.println(number); }

This reads each value from the array one at a time and stores it in the variable number. It is cleaner when you do not need to know which position you are at.

Create arrays of different data types

Arrays work with any data type Java supports. You can create arrays of integers, decimals, text, booleans, or even objects. The syntax stays the same — only the data type changes.

Here are common examples:

  • double[] prices = {9.99, 19.99, 29.99}; — stores decimal numbers
  • boolean[] flags = {true, false, true}; — stores true or false values
  • String[] colors = {"red", "green", "blue"}; — stores text
  • char[] letters = {'a', 'b', 'c'}; — stores single characters (note the single quotes)

You can also create multidimensional arrays — arrays that contain other arrays. A 2D array (two dimensions) looks like a table with rows and columns:

int[][] grid = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

To access a value in a 2D array, use two indices: grid[0][1] gets the value 2 (first row, second column).

Frequently Asked Questions

What happens if I try to put the wrong data type into an array?

Java will not let you. If you declare an array as int[], you can only store integers in it. If you try to put a decimal number or text into it, Java will show an error before your program even runs. This protection prevents bugs.

Can I change the size of an array after I create it?

No. Arrays have a fixed size that you set when you create them. If you need to add or remove items later, you have to create a new, larger array and copy the values over. For situations where the size changes often, Java offers ArrayList, which grows and shrinks automatically.

Why does array numbering start at 0 instead of 1?

This is how Java (and most programming languages) work internally. The index represents the distance from the start of the array, so the first item is 0 positions away. It takes practice to get used to, but it becomes automatic quickly.

What is the difference between declaring an array and creating it?

Declaration tells Java the type and name but does not build it in memory. Creation actually builds it using the new keyword. You can declare without creating, but you cannot use the array until both steps are done.