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.

Note*: 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 size of array in declaration.

Declaration only doesn’t create array object in heap.

Constructing an  Java Array

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

Note: Size of the array means how much element an array can contains.

One Dimensional Array

New keyword will be used to construct one/multi dimensional array.

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

Two Dimensional Array

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

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

Note: Only 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 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 can not be included in Declaration.
  • 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]);



Series Navigation<< Java Thread TutorialJava Enum (Enumerations) >>

0 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.


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.