Integer Array Initialization in Java
How to Initialize array ?
Initialization means assigning the values to the array at the time of declaraion. It means declaration and assignment of the array is done in one step.This is how the initialization of the array is done in Java
Syntax to initialize array in Java
data_type arrayName[] ={element1,element2,..............element_n};
Array initialization is shown in given step.
int arr[ ]={10,20,30};
Example of initializing integer array in Java:
//Java program to initialize integer array.
public class IntegerArrayInitialization {
public static void main(String[] args) {
int arr[]={10,20,30}; // initializing array
System.out.println("value at arr[2] is = "+arr[2]);
}//end of main method
}//end of class IntegerArrayInitialization
Output:
In the above example, we have initialized array with three elements and displayed element of index number 2.