Array Declaration

Started by VelMurugan, Nov 21, 2008, 10:01 PM

Previous topic - Next topic

VelMurugan

Array Declaration

As we declare a variable in Java, An Array variable is declared the same way. Array variable has a type and a valid Java identifier i.e. the array's type and the array's name. By type we mean the type of elements contained in an  array. To represent the variable as an Array, we use [] notation. These two brackets are used to hold the array of a variable.

By array's name, we mean that we can give any name to the array, however it should follow the predefined conventions. Below are the examples which show how to declare an array :-

int[] array_name;     //declares an array of integers
String[] names;
int[][] matrix;    //this is an array of arrays


It is essential to assign memory to an array when we declare it. Memory is assigned to set the size of the declared array. for example:

Quoteint[] array_name = new int[5];

Here is an example that creates an array that has 5 elements.

public class Array
{
   public static void main(String[] args)
   {
      int[] a = new int[5];
   }
}