Array concept in java

An array is a collection of similar data types.  Array is a container object which hold values of a data type. It is specific size and type since it is initial.  The index of array start from  0 to size of array -1.

Declaration of array

syntax:

datatype []  variable_name;
or 
datatype variable_name[];

Example :

byte [] arr;
byte arr[];

int[] arr;
int arr[];

double [] arr;

Initialization of array

we use keyword new to initial array;

Example:

int[] arr = new int[5];
// 5 is size of array 

or 

int[] arr = {1,2,3,4,5,6}

Access element of array

Array is accessed  element by index . As mention above index of is array is start form 0 to size -1.

arrayName[n-1];
// access at element n th

Example:

to access element 3th of array x[]

int x = {1,2,3,4,5,6,7,8};

System.out.print("The value at element 3 th" +x[2]);

Output:

The value at element 3th =3

 

 

 

 

Add a Comment

Your email address will not be published. Required fields are marked *