Java Array Tutorial

Java Array is a data structure type which is used to store multiple variables of the same type. Arrays can hold Primitives as well as Object.

An array will always be an Object in Heap. No matter what it store, primitives or Object.

Array Declaration (Syntax)

Array of Primitives

		//Single Dimensional Array
		int[] arr;  //recommended
		int arr[];

		//Multi Dimensional Array
		int[][] arr;  //recommended
		int arr[][];
		int[] arr[];Array Of Objects

Array Of Object

		//Single Dimensional Array
		String[] arr; //recommened
		String arr[];

		//Multi Dimensional Array
		String[][] arr; //recommened
		String arr[][];
		String[] arr[];
 Note: You cannot include the size of an array in the declaration.

Declaration only doesn’t create array object in heap.

Constructing a Java Array

This is the step where array object will be created on the heap. As once created the size of the array can not be changed, hence the size of the array needs to be provided at the time of constructing it. This size will be used by JVM while creating an array object on Heap.

Size of the array means how much element an array can contain.

One Dimensional Array

New keyword will be used to construct one/multidimensional array.

		int[] arr; //declares a new array
		arr = new int[10]; One Dimensional Array

Two Dimensional Array

These are the array of arrays. So a two-dimensional array is an array of arrays of int. So when you say, you are creating a two-dimensional array of Long primitive it means it will be an array containing multiple elements which in itself an array of Multidimensional Array

		int[][] arr;
		arr = new int[10][];

Only the first part(First Dimension is required) needs the size and not the all.

Initializing Array

Once arrays created and space assigned to it next thing would be to add elements in it. Initialization of array is the place where we can do this(Adding Element in an array).

Single Dimensional Array

		int[] arr  = new int[10];
		arr[0] = 0;
		arr[0] = 1;
		int[][] arr  = new int[10][]; //   Multi Dimensional Array   
		arr[0][0] = 0;
		arr[0][1] = 1;


Java Array Cheat-sheet

  • Arrays are always created in HEAP.
  • Arrays can contain both Primitives and Objects.
  • Size of Array cannot be included in Declaration.
  • The declaration doesn’t create array object in heap.

Array Example

public class JavaArrayBasic {

	public static void main(String args[]) {

		 * Array needs to be initialized before it can be used
		int intArr[] = { 1, 2 };
		int intArr1[];

		// intArr1 can not be used @ this place as it has not been initialized yet

		 * intArr = {1,2}; Array constants can only be used in initializers, So
		 * above line will give compile time error

		for (int i = 0; i < 2; i++)
			System.out.println("Value in array @ index" + i + " is "
					+ intArr[i]);



<< Java Thread Tutorial Java Enum (Enumerations) >>

17 Comments Java Array Tutorial

  1. Debajyoti

    how to find a unique row from a 2D array where the row is not equals to other row or not reverse of any other row

  2. Anurag Singh

    Hello sir,

    this java array post is rally helpful for everyone who want to learn java array easily.
    you explained very well.
    please keep posting.
    thank you.

  3. nisha

    Awesome, very well written article. Many important points are covered here. your article is very petrified me in the learning process and provide
    additional knowledge to me , maybe I can learn more from you, thank you


Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.