How to Initialize an Array in Java?Last Updated : 7 Jan 2026 In Java, an array is a data structure that allows us to store multiple values of the same type in a single variable. It provides a convenient way to work with collections of data. Before initialize an array, its declaration is must. To read more Java Arrays Array DeclarationHere, the datatype is the type of element that we want to store in the array, square bracket[] is for the size of the array, and arrayName is the name of the array. Array InitializationIn order to store values in the array, it is required to initialize it after declaration. In Java, there is more than one way of initializing an array which is as follows: InstantiationDeclaration and InstantiationThe array is constructed with a certain length with this type of declaration, and the components are initialised with default values (such as 0 for numeric types and null for reference types). Declaration and Initialization of an Array with ValuesIn this type of declaration, the array is created and initialized with specific values in single statement. Declaration and Initialization of a Multidimensional ArrayA multidimensional array having several dimensions, such as a 2D or 3D array, is created by this kind of statement. The array's elements can each hold multiple values. Declaration of an Empty ArrayWith this kind of declaration, a zero-sized array is created. It may come in handy when you want an empty array with no elements, depending on the situation. Example: Without Assigning ValuesIn the following program, we have taken an integer array of length 5. It means that we can store 5 elements in the array. Note that we have not assigned any value in the array. While we print the given array to the console, it prints the default values to the console i.e. 0. ExampleCompile and RunOutput: 0 0 0 0 0 Example: Initialize an Array After the DeclarationIn the following program, first we have declared an array (numbers[]) of type int. After that, we have used the new keyword to creates a new integer array with the specified values. While we print the given array to the console, it prints the specified values to the console i.e. 0. ExampleCompile and RunOutput: 22 33 44 55 66 Example: Declaration, Initialization, and Assigning Values in Single StatementIn the following program, we have declared, initialize an array and also assigned values in a single statement. ExampleCompile and RunOutput: 22 33 44 55 66 Note that we can write the following statement in short as follows: Both statements create an integer array in Java, but there are subtle differences in syntax, usage, and flexibility. Short Syntax: int[] numbers = {………..};
Full Syntax (Explicit): int[] numbers = new int[]{……….};
Which one to use?
|
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India