Array
An array is an object which contains only similar types of data.
Key Points:-
- The array has a fixed size. It takes memory when it is initialized.
- It is index index-based data structure. Each index of the array takes memory next to the previous index.
- Array object inherits Object class and implements Serializable and Cloneable interface.
- The array starts with a 0 index.
- We can randomly access any index data in Java.
How to declare and initialize an array
Below are the ways to declare the array in Java:
//dataType arrayVariableNAme [ ] ;
int integerAr [ ];
//dataType [ ] arrayVariableNAme ;
int [ ] integerAr ;
Initialization of Array:
//arrayVariableNAme = new datatype [size];
integerAr = new int [10];
//arrayVariableNAme ={element1 ,element2,element3….};
integerAr = {1,4,6,8,10};
//inintialization at the time of delearation
int [ ] integerAr =new int [10];
int [ ] integerAr ={1,4,6,8,10};