Java Arrays
An array is a collection of similar data types. Array is a container object that hold values of homogeneous type. It is also known as static data structure because size of an array must be specified at the time of its declaration.
Array starts from zero index and goes to n-1 where n is length of the array.
In Java, array is treated as an object and stores into heap memory. It allows to store primitive values or reference values.
Array can be single dimensional or multidimensional in Java.
Features of Array
Single Dimensional Array
Single dimensional array use single index to store elements. You can get all the elements of array by just increment its index by one.
Array Declaration
Syntax :
datatype[] arrayName;;
or
datatype arrayName[];
Java allows to declare array by using both declaration syntax, both are valid.
The arrayName can be any valid array name and datatype can be any like: int, float, byte etc.
Example :
int[ ] arr;
char[ ] arr;
short[ ] arr;
long[ ] arr;
int[ ][ ] arr;
Initialization of Array
Initialization is a process of allocating memory to an array. At the time of initialization, we specify the size of array to reserve memory area.
Initialization Syntax
arrayName = new datatype[size]
Powered by Froala Editor