---
description: In this tutorial, learn How to Declare, Create, Initialize Array in JAVA with Examples. Also understand Pass by reference and Multidimensional arrays. What is an Array? An array is a very common type of data structure wherein all elements must be of the same data type.
title: Arrays in Java
image: https://www.guru99.com/images/arrays-in-java.png
---

 

[Skip to content](#main) 

**⚡ Smart Summary**

Arrays in Java are fixed-size, index-based containers that store elements of one data type in contiguous memory, giving constant-time access, built-in bounds checking, and a length field that make them the foundation for most standard-library collections.

* 📦 **Fixed size:** An array holds a set number of elements of a single type.
* 🔢 **Zero-based index:** Elements run from 0 to length minus one.
* 🧩 **Two types:** Java has single-dimensional and multidimensional arrays.
* 🛠️ **Three steps:** Every array is declared, constructed with `new`, and initialised.
* 🛡️ **Bounds checking:** The JVM throws `ArrayIndexOutOfBoundsException` past the last slot.
* ↩️ **By reference:** Element changes made inside a method persist for the caller.
* 📏 **Length field:** Every array exposes `length` for safe iteration.

[ Read More ](javascript:void%280%29;) 

![Arrays in Java](https://www.guru99.com/images/arrays-in-java.png)

## What Is a Java Array?

A **Java array** is a fixed-size structure that stores elements of one data type in contiguous memory. Each element is reached by a zero-based index, so the first sits at `0` and the last at `length - 1`. Arrays inherit from `Object` and implement `Serializable` and `Cloneable`, and can hold primitives or object references.

Arrays replace long runs of numbered variables. Consider six independent integers:

x0 = 0;
x1 = 1;
x2 = 2;
x3 = 3;
x4 = 4;
x5 = 5;

The same six values live in a single array of length six:

x[0] = 0;
x[1] = 1;
x[2] = 2;
x[3] = 3;
x[4] = 4;
x[5] = 5;

A [loop](https://www.guru99.com/foreach-loop-java.html) variable can drive the index in brackets, so three lines process every element instead of six copies:

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

## Types of Array in Java

Java arrays come in two shapes:

1. **Single-dimensional array:** a flat list of elements.
2. **Multidimensional array:** an array of arrays, used for grids and matrices.

## Array Variables: Declare, Construct, and Initialise

Using an array is a **three-step process**: declare, construct with `new`, then initialise.

### 1) Declare the Array

**Syntax:**

<elementType>[] <arrayName>;

**or** (the older C-style form, still valid):

<elementType> <arrayName>[];

**Example:**

int[] intArray;   // canonical form: brackets on the type
int intArray2[];  // legacy C-style form, discouraged in new code

### 2) Construct the Array

**Syntax:**

arrayName = new dataType[size];

**Example:**

intArray = new int[10]; // defines that intArray will store 10 integer values

**Declaration and construction combined:**

int[] intArray = new int[10];

### 3) Initialise the Array

intArray[0] = 1; // assigns 1 to the first element (index 0)
intArray[1] = 2; // assigns 2 to the second element (index 1)

**Declare and initialise in one step** using an array literal:

int[] intArray = {1, 2, 3, 4};
// initialises an integer array of length 4 where the first element is 1,
// the second element is 2, and so on.

### RELATED ARTICLES

* [Introduction to Java ](https://www.guru99.com/introduction-to-java.html "Introduction to Java")
* [Java BufferedReader: How to Read a File with Example ](https://www.guru99.com/buffered-reader-in-java.html "Java BufferedReader: How to Read a File with Example")
* [Scala Tutorial ](https://www.guru99.com/scala-tutorial.html "Scala Tutorial")
* [Selection Sorting in Java Program with Example ](https://www.guru99.com/selection-sorting-java.html "Selection Sorting in Java Program with Example")

## First Array Program in Java

**Step 1)** Copy the following code into an editor:

class ArrayDemo {
    public static void main(String args[]) {
        int array[] = new int[7];

        for (int count = 0; count < 7; count++) {
            array[count] = count + 1;
        }

        for (int count = 0; count < 7; count++) {
            System.out.println("array[" + count + "] = " + array[count]);
        }

        // System.out.println("Length of Array  =  " + array.length);
        // array[8] = 10;
    }
}

**Step 2)** Save, compile, and run the code. Observe the output.

**Expected Output:**

array[0] = 1
array[1] = 2
array[2] = 3
array[3] = 4
array[4] = 5
array[5] = 6
array[6] = 7

**Step 3)** If `x` is an array reference, `x.length` returns its slot count. Uncomment the first commented line and re-run:

Length of Array  =  7

**Step 4)** Unlike C, Java bounds-checks every array access. Uncomment the second commented line and re-run:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
        at ArrayDemo.main(ArrayDemo.java:11)
Command exited with non-zero status 1

**Step 5)** The JVM throws `ArrayIndexOutOfBoundsException`. In C the same access would return garbage from adjacent memory instead of failing.

## Java Array: Pass by Reference

Arrays are passed to methods by reference. The method receives a copy of the reference, so element changes made inside the method are visible to the caller.

**Example: arrays are passed by reference.**

**Step 1)** Copy the following code into an editor:

class ArrayDemo {
    public static void passByReference(String a[]) {
        a[0] = "Changed";
    }

    public static void main(String args[]) {
        String[] b = {"Apple", "Mango", "Orange"};
        System.out.println("Before Function Call    " + b[0]);
        ArrayDemo.passByReference(b);
        System.out.println("After Function Call    " + b[0]);
    }
}

**Step 2)** Save, compile, and run the code. Observe the output.

**Expected Output:**

Before Function Call    Apple
After Function Call    Changed

## Multidimensional Arrays in Java

Multidimensional arrays are arrays of arrays. Add another set of square brackets for each dimension. To create a 2-D integer grid with four rows and five columns:

int[][] twoD = new int[4][5];

You only need to size the first (leftmost) dimension up front. The remaining dimensions can be allocated separately, and each row can have its own length, making Java 2-D arrays true _jagged arrays_.

**Example:**

public class Guru99 {
    public static void main(String[] args) {
        // Create a 2-dimensional array with 4 rows and 4 columns.
        int[][] twoD = new int[4][4];

        // Assign three elements to it.
        twoD[0][0] = 1;
        twoD[1][1] = 2;
        twoD[3][2] = 3;

        System.out.print(twoD[0][0] + " ");
    }
}

**Expected Output:**

1

## FAQs

📦 What is an array in Java?

A Java array is a fixed-size container that stores elements of one data type. Each element is accessed by a zero-based index, and every array exposes a length field with the slot count.

🛠️ How do you declare and initialise an array in Java?

Declare with int\[\] arr, construct with new int\[size\], then assign by index. To do all three in one line, use a literal such as int\[\] arr = {1, 2, 3, 4}.

🔢 What are the types of arrays in Java?

Java supports single-dimensional and multidimensional arrays. A single-dimensional array is a flat list, and a multidimensional array is an array of arrays used for tables, grids, and matrices.

📏 How do I find the length of an array in Java?

Use the length field, for example arr.length. It returns the slot count and works for any array type. Unlike String, arrays expose length as a field, not a method call.

🛡️ Why does Java throw ArrayIndexOutOfBoundsException?

The JVM bounds-checks every array access. Reading or writing an index below zero or at or above length throws ArrayIndexOutOfBoundsException, preventing the memory corruption bugs that plague C and C++.

↩️ Are arrays passed by value or by reference in Java?

Java passes arguments by value, but for arrays that value is a copy of the reference. The method acts on the same underlying array, so element changes inside the method are visible to the caller.

🤖 How does AI use arrays when generating Java code?

Machine learning code assistants recognise the declare-construct-initialise pattern and complete literals or loops. They also flag common off-by-one errors that would throw ArrayIndexOutOfBoundsException at runtime.

🛠️ Can GitHub Copilot help me work with arrays in Java?

Yes. GitHub Copilot scaffolds array declarations, loops, Arrays.sort calls, and 2-D matrix walks from a plain-language comment. Review the generated bounds before running against production data.

#### Summarize this post with:

ChatGPT Perplexity Grok Google AI 

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.** 

##### Sign up for the newsletter

Subscribe for Free 

You have successfully subscribed.  
Please check your inbox. 

![AI-Newsletter]() Chosen by over **350,000+** professionals 

[Scroll to top ](#wrapper)Scroll to top 

× 

Toggle Menu Close 

Search for: 

Search

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/arrays-in-java.png","url":"https://www.guru99.com/images/arrays-in-java.png","width":"700","height":"250","caption":"Arrays in Java","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/java-arrays.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/java-tutorials","name":"Java Tutorials"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/java-arrays.html","name":"Arrays in Java"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/java-arrays.html#webpage","url":"https://www.guru99.com/java-arrays.html","name":"Arrays in Java","dateModified":"2026-07-06T14:08:09+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/arrays-in-java.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/java-arrays.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/james","name":"James Hartman","description":"I am James Hartman, a seasoned professional in Oracle Certified Java Professional tutorials, specializing in crafting comprehensive guides to help you excel in your Java certification journey.","url":"https://www.guru99.com/author/james","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","url":"https://www.guru99.com/images/james-hartman-author-v2-120x120.png","caption":"James Hartman","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"Java Tutorials","headline":"Arrays in Java","description":"In this tutorial, learn How to Declare, Create, Initialize Array in JAVA with Examples. Also understand Pass by reference and Multidimensional arrays. What is an Array? An array is a very common type of data structure wherein all elements must be of the same data type.","keywords":"java","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/james","name":"James Hartman"},"dateModified":"2026-07-06T14:08:09+05:30","image":{"@id":"https://www.guru99.com/images/arrays-in-java.png"},"copyrightYear":"2026","name":"Arrays in Java","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is an array in Java?","acceptedAnswer":{"@type":"Answer","text":"A Java array is a fixed-size container that stores elements of one data type. Each element is accessed by a zero-based index, and every array exposes a length field with the slot count."}},{"@type":"Question","name":"How do you declare and initialise an array in Java?","acceptedAnswer":{"@type":"Answer","text":"Declare with int[] arr, construct with new int[size], then assign by index. To do all three in one line, use a literal such as int[] arr = {1, 2, 3, 4}."}},{"@type":"Question","name":"What are the types of arrays in Java?","acceptedAnswer":{"@type":"Answer","text":"Java supports single-dimensional and multidimensional arrays. A single-dimensional array is a flat list, and a multidimensional array is an array of arrays used for tables, grids, and matrices."}},{"@type":"Question","name":"How do I find the length of an array in Java?","acceptedAnswer":{"@type":"Answer","text":"Use the length field, for example arr.length. It returns the slot count and works for any array type. Unlike String, arrays expose length as a field, not a method call."}},{"@type":"Question","name":"Why does Java throw ArrayIndexOutOfBoundsException?","acceptedAnswer":{"@type":"Answer","text":"The JVM bounds-checks every array access. Reading or writing an index below zero or at or above length throws ArrayIndexOutOfBoundsException, preventing the memory corruption bugs that plague C and C++."}},{"@type":"Question","name":"Are arrays passed by value or by reference in Java?","acceptedAnswer":{"@type":"Answer","text":"Java passes arguments by value, but for arrays that value is a copy of the reference. The method acts on the same underlying array, so element changes inside the method are visible to the caller."}},{"@type":"Question","name":"How does AI use arrays when generating Java code?","acceptedAnswer":{"@type":"Answer","text":"Machine learning code assistants recognise the declare-construct-initialise pattern and complete literals or loops. They also flag common off-by-one errors that would throw ArrayIndexOutOfBoundsException at runtime."}},{"@type":"Question","name":"Can GitHub Copilot help me work with arrays in Java?","acceptedAnswer":{"@type":"Answer","text":"Yes. GitHub Copilot scaffolds array declarations, loops, Arrays.sort calls, and 2-D matrix walks from a plain-language comment. Review the generated bounds before running against production data."}}]}],"@id":"https://www.guru99.com/java-arrays.html#schema-1133647","isPartOf":{"@id":"https://www.guru99.com/java-arrays.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/java-arrays.html#webpage"}}]}
```
