---
spec: JLS
title: "The Java Language Specification"
edition: "Java SE 10 Edition"
java_version: 10
chapter: "Chapter 10. Arrays"
section: "10.2"
heading: "10.2. Array Variables"
parent: "Chapter 10. Arrays"
source: https://docs.oracle.com/javase/specs/jls/se10/html/jls-10.html#jls-10.2
id: "10.2"
url: https://jvm.obfuscated.software/jls/se10/10.2.html
raw: https://jvm.obfuscated.software/jls/se10/10.2.md
---

# 10.2. Array Variables

A variable of array type holds a reference to an object. Declaring a variable of array type does not create an array object or allocate any space for array components. It creates only the variable itself, which can contain a reference to an array. However, the initializer part of a declarator ([§8.3](8.3.md), [§9.3](9.3.md), [§14.4.1](14.4.1.md)) may create an array, a reference to which then becomes the initial value of the variable.

**Example 10.2-1. Declarations of Array Variables**

```
int[]     ai;        // array of int
short[][] as;        // array of array of short
short     s,         // scalar short
          aas[][];   // array of array of short
Object[]  ao,        // array of Object
          otherAo;   // array of Object
Collection<?>[] ca;  // array of Collection of unknown type
```

The declarations above do not create array objects. The following are examples of declarations of array variables that do create array objects:

```
Exception ae[]  = new Exception[3];
Object aao[][]  = new Exception[2][3];
int[] factorial = { 1, 1, 2, 6, 24, 120, 720, 5040 };
char ac[]       = { 'n', 'o', 't', ' ', 'a', ' ',
                    'S', 't', 'r', 'i', 'n', 'g' };
String[] aas    = { "array", "of", "String", };
```

The array type of a variable depends on the bracket pairs that may appear as part of the type at the beginning of a variable declaration, or as part of the declarator for the variable, or both. Specifically, in the declaration of a field, formal parameter, or local variable ([§8.3](8.3.md), [§8.4.1](8.4.1.md), [§9.3](9.3.md), [§9.4](9.4.md), [§14.4.1](14.4.1.md), [§14.14.2](14.14.2.md), [§15.27.1](15.27.1.md)), the array type of the variable is denoted by:

- the element type that appears at the beginning of the declaration; then,
- any bracket pairs that follow the variable's *Identifier* in the declarator (not applicable for a variable arity parameter); then,
- any bracket pairs that appear in the type at the beginning of the declaration (where the ellipsis of a variable arity parameter is treated as a bracket pair).

The return type of a method ([§8.4.5](8.4.5.md)) may be an array type. The precise array type depends on the bracket pairs that may appear as part of the type at the beginning of the method declaration, or after the method's formal parameter list, or both. The array type is denoted by:

- the element type that appears in the *Result*; then,
- any bracket pairs that follow the formal parameter list; then,
- any bracket pairs that appear in the *Result*.

We do not recommend "mixed notation" in array variable declarations, where bracket pairs appear on both the type and in declarators; nor in method declarations, where bracket pairs appear both before and after the formal parameter list.

**Example 10.2-2. Array Variables and Array Types**

The local variable declaration statement:

```
byte[] rowvector, colvector, matrix[];
```

is equivalent to:

```
byte rowvector[], colvector[], matrix[][];
```

because the array type of each local variable is unchanged. Similarly, the local variable declaration statement:

```
int a, b[], c[][];
```

is equivalent to the series of declaration statements:

```
int a;
int[] b;
int[][] c;
```

Brackets are allowed in declarators as a nod to the tradition of C and C++. The general rules for variable declaration, however, permit brackets to appear on both the type and in declarators, so that the local variable declaration statement:

```
float[][] f[][], g[][][], h[];  // Yechh!
```

is equivalent to the series of declarations:

```
float[][][][] f;
float[][][][][] g;
float[][][] h;
```

Because of how array types are formed, the following parameter declarations have the same array type:

```
void m(int @A [] @B []  x) {}
void n(int @A [] @B ... y) {}
```

And perhaps surprisingly, the following field declarations have the same array type:

```
int @A [] f @B [];
int @B [] @A [] g;
```

Once an array object is created, its length never changes. To make an array variable refer to an array of different length, a reference to a different array must be assigned to the variable.

A single variable of array type may contain references to arrays of different lengths, because an array's length is not part of its type.

If an array variable `v` has type A`[]`, where A is a reference type, then `v` can hold a reference to an instance of any array type B`[]`, provided B can be assigned to A ([§5.2](5.2.md)). This may result in a run-time exception on a *later* assignment; see [§10.5](10.5.md) for a discussion.
