Home » java » Array

Array

Array is the collection at similar elements. All array element are stored in continues memory location.

Declaration

Syntax

Example

In above declaration age is a reference variable which will hold reference of array same for price and for names.

Array construction

Array uses new operator to dynamically allocating memory locations.
Syntax

Example

new operator will dynamically allocate memory for 5 integer values that is 5*4=20 bytes continuously.

Array uses zero based indexing where first element is stored in zero location. Last element is stored in array size-1 location.

We can assign values in array element one by one as below

Array initialization

Array declaration and initialization can be done in one statement
syntax

Example

Accessing array element: array element can be accessed by its index value.

Syntax

Example age[0] access all first value of array we can use loop
We can print all values is age below

Example

Result

Please follow and like us:
error