Each and every element in an array is identified through a non negative number called index. The size of an array is fixed and can not be changed
An array has a final field called length, which denotes the number of elements an array can accommodate. The first element is always at index 0 and the last element at length – 1.
Declaring an array variable
An array can be declared using following syntax.
[] arrayName ;
arrayName [];
Here type can be any primitive or reference type.
Note: Array size is not defined here. This array variable can be assigned to an array of any length.
Declaring an array variable does not actually create an array nor allocate any memory. It just creates a variable which can hold reference to the array.
An array variable declared as a member variable will be automatically initialized to default value null, while local array variable not.
Creating an Array
Since arrays are java objects, it can be created using new operator using following syntax.
arrayName = new [];
Here size must be byte, char, short or int. Any attempt to create an array with long will result in compilation error. If the array size is negative, NegativeArraySizeException is thrown.
When create arrays are initialized automatically to their default values.
Note: In java, it is perfectly legal to create array with size 0.
Java array creation examples are given below.
myIntArray = new int[10];
myStringArray = new String[10];
myObjectArray = new MyObject[10];
Array declaration and creation can be combined into one statement as follows.
int myIntArray = new int[10];
String myStringArray = new String[10];
MyObject myObjectArray = new MyObject[10];
Initializing an array
Once created, array elements can be initialized explicitly using loop or initialization can be combined with the array creation using following syntax.
[] arrayName = { };
Following is the example of the same.
int [] myIntArray = {1,2,3,4};
Note: initialization list can be legally terminated by comma. For example
int [] myIntArray = {1,2,3,4,};
Accessing elements of an array
Individual elements of an array can be accessed by specifying index with [] operator, with the following syntax.
arrayName[]
Here index can be any expression, which evaluates to non negative int value. Array index always starts at 0 and ends at arrayName.length – 1. Array index is automatically checked to ensure that it is well within the bounds. If index is less than 0 or grater than arrayName.length, an ArrayIndexOutOfBoundException is thrown.
Note: [] operator is not used when manipulating the array reference. For example, when array reference is passed to another method.
Multidimensional Array
In java, multidimensional ( array of arrays) can be defined using following syntax.
arrayName[]…[];
[]…[] arrayName[];
Where []…[] denotes number of dimension.
We can also combine array declaration and array construction in one statement as follows.
[]…[] arrayName = new []…[];
Example of multidimensional array
int myIntArray[][] = new int[5][5];
String myStringArray[][] = new String[5][5];
Multidimensional can also be created and initialized in single expression as follows.
int myIntArray[][] = {
{1,2,3},
{4,5,6}
Note: While constructing multidimensional array, length of deeply nested array can be omitted which will not be constructed.
For example,
int myIntArray[][][] = new int[1][2][];
Deeply nested array can be constructed later on using loop.
Example program of Multidimensional array
Class MultidimensionalArrayExample{
Public static void main(String args[]){
int myIntArray[][] = { {1,2},{3,4}};
for(int i = 0; i < myIntArray.length; i++){
for(int j=0; j < myIntArray[i].length; j++){
System.out.println(myIntArray[i][j]);
}
}
}
The above given program will print elements of multidimensional array to console.
Anonymous Array
In java it is perfectly legal to create an anonymous array using the following syntax.
new [] { };
Anonymous array example
new int[]{1,2,3};
The above given example creates a nameless array and initializes it. Here, neither name of the array nor the size is specified. It also creates a array which can be assigned to reference or can be passed as a parameter to any method.
Example of Anonymous array
public AnonymousArrayExample{
public static void main(String args[]){
System.out.println(“Length of array is “ + findLength(new int[]{1,2,3}));
public static findLength(int[] array){
return array.lentgh;
The above given program will print the length of the anonymous array which is being passed to the static method.
Rahim Vindhani has sinced written about articles on various topics from Computers and The Internet. . Rahim Vindhani's top article generates over 5400 views. to your Favourites.
Check Your School Grades Follow these 10 tips and within next to no time the quality of your sleep will improve, you will feel much more awake and active during the day and your grades will improve