Java


Java Tutorial


Admission Enquiry Form

  

Integer Array in Java



What is Array in Java ?

Array is a technique which is used to store more than one value but of similar type.It stores the values in contigeous or adjacent memory location.Every element of an array having its unique index number.

Instead of declaring individual variable, such as number0, number1, ..., and number99, we can declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

Syntax to declare array in Java

data_type arrayName[] = new data_type[arraysize];

This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid Java data type. For example, to declare a 3-element array called num of type int, use the following statement:

int arr[ ]=new int[3];




Example of integer array in Java:

//Java program of integer array
public class IntegerArray {
public static void main(String[] args) {

int arr[]=new int[3];
arr[0]=10;
arr[1]=20;
arr[2]=30;
System.out.println("value at arr[1] is = "+arr[1]);
} //end of main method
}//end of class IntegerArray


Output:

value at arr[1] is = 20

In the above example, we have declared array of size 3 , then we have assigned value to every location of the array sequence wise, then finally we have displayed element of index number 1.