Array in Java

An Array is a collection of similar types of data with contiguous memory location. An array can store more than one value of similar data type. Here we are going to learn about array in Java.

Java Array

Array in Java:-

An array in java is not the same as an array in C or C++. An array is an object in java. following are the properties of the array in java. At the time of array creation, it’s compulsory to specify the size of the Array. The maximum allowed Array size in Java is 2147483647 (max value of int data type).

  • Arrays elements are stored in contagious memory location
  • The length of the Array is fixed. We can’t change the length of an Array in java after the declaration of the array.
  • Array index starts from 0 and it goes to till length -1. for single dimension 1[D] array.

Array declaration in java:-

In java every Array is an object. we can create an array by using new() .

In java we can declare array in the following way.

Syntax for array declaration in java-

Data type Array name[] = new Data type [Length of Array];                                                                  
Data type[] Array name = new Data type [Length of Array];

Example of Array declaration in java-

int array[] = new int[4];
String[]  str = new String[4];
  • we can also declare a Array with size 0.
int array[] = new int[4];
  • we can’t specify array size with a negative numbers.
  • we can only use byte, short, char, int data type of size of array

Initializing an array in java:-

We can initialize an array in java at the time of array declaration. Following are examples of array initialization in java.

Examples of Array initialization:-

int array[]={10,20,30,40,50};
String array[]={"THE","CODE","DATA"};

Array Initialization, Creation and Declaration in a single line:-

We can also declare, create and initialize an array in a single line.

One-Dimensional array Creation, Initialization and Declaration in a single line:-

int [] x = {1, 2, 3, 4};
char [] ch = { 'j', 'a', 'v', 'a'};
String [] s = {"The", "code", "Data"};

Multi-Dimensional array Creation, Initialization and Declaration in a single line:-

We can also declare, create and initialize Multi-Dimensional array (2D array) in a single line.

int [][] a = {{1,2,3,4},{5,6,7,8}};

Valid Array Declaration in Java:-

int [] array = new int[];Invalid
int [] array = new int[5];Valid
int [][] array = new int[][];valid
int [][] array = new int[5][5];Invalid
int [][][] array = new int[5][];Invalid

Length of array:-

We can find length of array in java by using final variable length.

int [] a = new int[10];
int size = a.length;

Array input in java:-

For taking array input in java, we have to take input for all elements of an array using Scanner. We can use a loop to iterate the array’s index value and print elements of the array.

Array Input Output Java Example

package learn;
import java.util.*;
public class ArrayInput {
public static void main(String[] args) {
    Scanner sc = new Scanner(;
    System.out.println("Enter the size of array");
    int n=sc.nextInt();
    int[] array= new int[n];
    System.out.println("Enter the elements of array");
    for(int i=0;i<n;i++) {

    System.out.println("Array element entered by you");
    for(int i=0;i<n;i++) {

Here we are first taking input for the size of the array. after that, we are creating an int array of size input by the user. After that, we are running a for loop for taking inputs. After that, we are running a for loop to print elements of the array one by one.

Output for java array input program:-

Array input Java

Types of array in java:-

There is two types of array in java.

  • One Dimensional Array
int array[]=new int[5];
  • Multi Dimensional Array
int array2D [][]= new int [2][3];
int array3D [][][] = new int[2][3][4];

We already seen the examples and working of one dimensional array and for multi dimensional array you can check here –

Similar Java Tutorial:-

Leave a Comment