Java Array Tutorial

This entry is part 33 of 36 in the series Core Java Course

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.java Multidimensional Array

		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.
Series Navigation<< Java Thread TutorialJava Enum (Enumerations) >>
By | 2012-04-19T12:03:29+00:00 April 19th, 2012|Core Java|17 Comments

About the Author:

17 Comments

  1. doniya September 15, 2014 at 10:15 am - Reply

    thanks.

  2. trapti September 25, 2014 at 11:03 am - Reply

    please provide some material about array,some programs or somthing

    • Shyamala Mahadevappa September 13, 2015 at 11:07 am - Reply

      Please provide somw basics about java programming..

      • J Singh September 13, 2015 at 12:21 pm - Reply

        Hi Shyamla,

        Basics as in??

        Regards

  3. ravi November 4, 2014 at 10:03 am - Reply

    pls provide deeply information of array

  4. RADHIKA December 31, 2014 at 10:25 am - Reply

    Is it possible to create dynamic array without using arraylist?
    If yes, how?

    • dsb February 6, 2017 at 10:46 am - Reply

      No it wont possible to create dynamic array.To overcome from this problem we have to know Collection.

  5. parveen saini February 18, 2015 at 7:03 pm - Reply

    how print arrays program

    • ghghghghghghghghghghgh December 19, 2015 at 1:14 pm - Reply

      nvccccccccccccccccccccccccccccccccccccccccccccc

      • Gandhi April 21, 2016 at 10:51 am - Reply

        Don’t post unuseful comments, This should be taken care from site developer and review team..!

    • Anonymous March 11, 2016 at 11:01 am - Reply

      You can print it using For Loop

      int arr[5];
      for(int i=0;i<5;i++)
      {
      System.out.println(arr[i]);
      }

    • MODI April 21, 2016 at 10:53 am - Reply

      @PARVEEN SAINI , Please provide full details. What you want to do ? Asking for PRINTING arrays program..!!!

  6. sabitha February 24, 2015 at 7:55 am - Reply

    i learnt core java from u thank u so much

  7. Debajyoti May 15, 2015 at 4:29 am - Reply

    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

  8. CHIRANJEEVI December 2, 2015 at 8:45 am - Reply

    WE WANT CLEAR I MEAN BREAFLY FILE FOR READING…………….

  9. joshi December 18, 2015 at 9:08 am - Reply

    very good for begining thanks

  10. Anurag Singh May 16, 2017 at 7:31 am - Reply

    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