In previous article, I mentioned Various Java programming examples with output but in this article, I have mentioned how we can Declare and initialize Array in java or how we can create array in Java with a working example.

When we are creating arrays, you must know what type of element you will store in an array and maximum number of elements allowed to be stored in them.

Declare an array in Java

In java, there are multiple ways to declare an array, here are the syntax of all ways:

dataType[] arrayName; 

//OR
dataType arrayName[]; 

// OR
dataType []arrayName;

Where dataType, can be int, string etc and arrayName = name of the array.

So, for example, if we want to declare an array with data type as int, it can be done as below:

int[] arr;
//OR
int []arr;
//OR
int arr[];

// in all above example, we have declared array and will initialize later

as you can see from the above example, we have just declared an array and we will initialize it later, and we haven't mentioned any size of array, so basically it is a dynamic array.

Which tells the compiler, that this array (arr) will hold data of type int.

Initialize Array in Java

In the previous section, we just declared an array but now, we will initialize it also, so we can use it to store values.

Syntax:

DataType[] variableName = new DataType[size];

DataType[] is the type of the variable called variableName ("variableName" is called the identifier).

The literal "DataType" is the base type, and the brackets mean this is the array type of that base.

Size = the number of elements that will be stored in array

So, considering all these we can have the below example to declare and initialize an array

int[] intArray = new int[10];

in the above example, we have intArray which can store a maximum of 10 elements to int type.

OR

We can also divide the above steps, into 2 lines of code

int[] intArray;          //declaration
intArray = new int[10];  //instantiation

Once the array is created, you can store values in it with values as follows:

intArray[0] = 1;
intArray[1] = 5;
intArray[2] = 44; //....and so on until all elements are initialized.

The expression in the square brackets above is called the index of the array.

The index of the array is used to access the actual value of the elements i.e. the above array (intArray) of 10 int elements will have indices numbered from 0 to 9.

Array Example in Java

Now, we have learned about how to declare and initialize an array in Java, let's take a look at a complete Array example in Java

public class Main
{
	public static void main(String[] args) {
		int[] intArray;          //declaration
        intArray = new int[5];   //instantiation
        intArray[0] = 10;        //initialization
        intArray[1] = 11;
        intArray[2] = 12;
        intArray[3] = 13;
        intArray[4] = 15;
        System.out.println("intArray[0] = " + intArray[0]);  //accessing and printing array elements
        System.out.println("intArray[1] = " + intArray[1]);
        
        int [] secondArray = {1,3,5,7};    //initialization with array literal
        System.out.println("secondArray[0] = " + secondArray[0]);
        System.out.println("secondArray[1] = " + secondArray[1]);
        System.out.println("secondArray[2] = " + secondArray[2]);
        System.out.println("secondArray[3] = " + secondArray[3]);
	}
}

Output:

intArray[0] = 10
intArray[1] = 11
secondArray[0] = 1
secondArray[1] = 3
secondArray[2] = 5
secondArray[3] = 7

array-declare-and-initiaze-in-java

In the above code, we have declared a Java array of int type, with size =5 and then we assigned values to each index of any array(intArray).

Once we have saved value, we are accessing index 0 and 1 value.

Then we used second way of initialization of an array with literal.

Using For Loop to Initialize array

Let's take a look at an another example, in which we will declare an array and the initialize it's value using Index location in for loop.

public class Main
{
	public static void main(String[] args) {
    	int[] intArray = new int[5];      //declare and instantiation
        for(int i=0;i<5;i++){        
             intArray[i] = i+1;     //initialization using for loop
        }
        System.out.println("intArray[0] = " + intArray[0]);
        System.out.println("intArray[1] = " + intArray[1]);
        System.out.println("intArray[2] = " + intArray[2]);
        System.out.println("intArray[3] = " + intArray[3]);
        System.out.println("intArray[4] = " + intArray[4]);
	}
}

Output:

intArray[0] = 1
intArray[1] = 2
intArray[2] = 3
intArray[3] = 4
intArray[4] = 5

That's it, hope it helps.

You may also like to read:

Static Class in Java

7 Ways to Merge or Split PDF Files in Java

Program to remove duplicate elements in an array in Java

Pyramid Triangle pattern programs in Java with explanation

Hello World program in Java (CMD and Eclipse Examples)

Leap year program in Java (multiple ways)