Arrays in Java: How to Declare and Initialize? A Complete Guide

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction: Why does this question trouble even experienced Java developers?

\n\n

If you've ever Googled "How do I declare and initialize an array in Java?", you are not alone. This question on Stack Overflow has gathered hundreds of thousands of views and continues to grow. It seems simple, right? But the devil, as always, is in the details. Java is a strongly typed language, and the syntax for arrays can confuse beginners.

\n\n

In this article, we will break down all possible ways to declare and initialize arrays in Java. You will learn how int[] arr differs from int arr[], how to correctly create arrays of objects, and how to avoid the NullPointerException trap. Let's go!

\n\n

1. The Basics: What is an Array in Java?

\n\n

An array in Java is a container that stores a fixed number of elements of the same type. Unlike Python or JavaScript, where arrays can be dynamic, in Java the size of an array is set at creation and cannot be changed.

\n\n
int[] numbers; // declaration\nnumbers = new int[5]; // initialization — allocating memory for 5 elements\n
\n\n

In the first line, we tell the compiler: "I want to create a variable that will reference an array of integers." In the second line, we allocate memory for 5 elements. Only after this is the array ready for use.

\n\n

2. Ways to Declare an Array

\n\n

In Java, there are two syntaxes for declaring an array. Both are valid, but the Java community (and Oracle's official coding standards) recommend the first option.

\n\n

Method 1: Data type + square brackets before the variable name

\n
int[] myArray;\nString[] names;\ndouble[] prices;\n
\n\n

Method 2: Square brackets after the variable name (C/C++ style)

\n
int myArray[];\nString names[];\ndouble prices[];\n
\n\n

Which one to choose? The first option reads more naturally: int[] myArray — "an array of integers named myArray". The second option can be confusing, especially when declaring multiple variables on one line:

\n\n
int[] arr1, arr2; // both variables are arrays\nint arr3[], arr4; // arr3 is an array, arr4 is a regular integer (error!)\n
\n\n

Therefore, always use type[] name.

\n\n

3. Array Initialization: 4 Proven Methods

\n\n

After declaration, the array needs to be initialized. Here's how to do it correctly.

\n\n

Method 1: Creating an empty array with a specified size

\n
int[] numbers = new int[10]; // array of 10 elements, all equal to 0 (default)\nString[] words = new String[5]; // array of 5 strings, all equal to null\n
\n\n

This method is suitable if you plan to fill the array later (e.g., in a loop). It's important to remember: for numeric types, default values are 0, for boolean — false, for reference types — null.

\n\n

Method 2: Initialization using an array literal

\n
int[] numbers = {1, 2, 3, 4, 5};\nString[] fruits = {"apple", "banana", "orange"};\n
\n\n

This is the shortest and most convenient method when you know all the elements in advance. Note: this syntax works only during declaration.

\n\n

Method 3: Anonymous array (without a variable name)

\n
int[] numbers = new int[]{1, 2, 3, 4, 5};\n
\n\n

This option is useful when you need to pass an array to a method without creating a separate variable:

\n\n
printArray(new int[]{10, 20, 30});\n
\n\n

Method 4: Initialization via a loop

\n
int[] squares = new int[10];\nfor (int i = 0; i < squares.length; i++) {\n    squares[i] = (i + 1) * (i + 1);\n}\n
\n\n

Ideal for dynamic filling, for example, calculating values or reading data from a file.

\n\n

4

Blogs

Book Recommendations