Home Java Beginners Arrayexamples Java Array Declaration



Java Array Declaration
Posted on: May 28, 2007 at 12:00 AM
In this section, you will learn how to declare array in Java.

Java 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:

int[] 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];
 }
}

Related Tags for Java Array Declaration:
javacidearrayiovariablesedtypenameriaidaielementelementsifietonotationldrackebracketsnotlivarusepeidentifierinnoasmntjclesdeclarevalidemmemeanotaacksamesatracoldkishaprerayeaandarreprsavatwssamssamrithavabablatieclidentidentprndonolo


More Tutorials from this section

Ask Questions?    Discuss: Java Array Declaration   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.