Concepts / Array Declarations and Creation

Array Declarations and Creation

An array literal, also called a list initializer, is a brace-enclosed list that gives initial values to a newly declared array variable, as in int[] squares = { 1, 4, 9, 16, 25, 36, 49 };.

  • Programming

Testing the Brace-List Assumption

When you first learn to create an array, you usually declare a variable and then fill each position one at a time. Once you learn that curly braces can hold a list of values, it is tempting to assume those braces work everywhere an array needs values. Many learners believe that a brace-enclosed list is a general way to hand any array variable, new or old, a fresh set of contents. This article tests that belief. The goal is to learn exactly where the list notation is allowed, what the other notation does, and how to tell which one a statement needs.

What do you think happens?

Given int[] squares = { 1, 4, 9, 16, 25, 36, 49 }; does the brace list { 2, 3 } also work in a later line that replaces the contents of squares, such as squares = { 2, 3 };?

  • Yes, the braces can give an existing array new values anywhere

  • No, the brace list works only in the declaration statement

Reveal answer
Answer:

No. The brace list works only in a declaration statement. It cannot be used in an assignment to a variable that already exists.

A declaration is the one moment when a variable is created and given its first value at the same time. An assignment works with a variable that already exists, and the brace list notation is not available there. Later sections show the notation that does work in that position.

Tracing the squares Declaration

Follow the declaration int[] squares = { 1, 4, 9, 16, 25, 36, 49 }; one step at a time. Keep one question in mind: once the declaration has finished, what does the variable squares refer to, and what does the array contain? You need both facts when you reach the rule about where literals may appear.

  1. 1
    The declaration names a variable squares with the type int[], meaning a variable intended to refer to an array of int values.
  2. 2
    The brace-enclosed list supplies seven initial values, separated by commas: 1, 4, 9, 16, 25, 36, 49.
  3. 3
    An array object is created and filled with those seven values, in the order written.
  4. 4
    The variable squares is set to refer to that newly created array object.
  5. 5
    After the statement finishes, squares refers to the array, and the array contains the seven values. The variable does not hold the values directly as one single number.
squaresvariable holding areferencenew int array object1, 4, 9, 16, 25, 36, 49refers to
When int[] squares = { 1, 4, 9, 16, 25, 36, 49 }; finishes, what does the variable squares hold, and where do the seven values live?

The diagram shows the key point. The variable squares is a reference to an array object that the declaration created. The seven values belong to that array object, not to the variable itself. This is why, later in the article, replacing the contents of squares means changing which array object the variable refers to, rather than typing a list directly into the variable.

Array Literals in Declarations

Definition

An array literal, also called a list initializer, is a brace-enclosed list of values, separated by commas, that gives initial values to a newly declared array variable. It sits on the same line as the declaration.

java
Notes on the code
  • int[] declares squares as a variable of array type holding int values
  • The braces hold the initial values, separated by commas
  • The seven values fill the array in the order written

Because the list is written in the declaration, the array is created and filled in the same step as the variable is introduced. You do not need a separate statement to create the array and another to fill each position.

Where the Brace List Is Not Allowed

The brace list is allowed only in a declaration statement. It cannot be used in an assignment to a variable that already existed. The same braces that work on the squares declaration line are rejected on a later line that tries to replace the contents of squares.

Before
int[] squares = { 1, 4, 9, 16, 25, 36, 49 };

Valid: the declaration creates squares and gives it its first value in one statement.

After
squares = { 2, 3 };

Not allowed: squares already exists, and a bare brace list cannot be assigned to it.

  • Common mistake

    Assuming braces work in any assignment

    What it looks like
    squares = { 2, 3 };
    Why it fails

    The list notation is available only in a declaration statement. An assignment works with a variable that already exists.

    Do this instead

    Use the new operator form to create a new array and assign it, as shown in the next section.

  • Common mistake

    Thinking the variable itself stores the values

    What it looks like

    Reading squares as if it were one number holding 1, 4, 9, and so on

    Why it fails

    The variable refers to an array object created by the declaration. The values belong to that array object.

    Do this instead

    Think of squares as a reference to an array, and the values as elements of that array.

  • Common mistake

    Putting the brace list on a separate line from the declaration, when the array literal is expected to sit with its declaration

The new Operator Form

The source describes a second, similar notation for creating a new array. It uses another form of the new operator, which both creates a new array object and fills it with values in one expression. Because it is an expression, it can be used in places where a list initializer cannot, including assignments. The source notes that this syntax looks unusual and resembles the syntax for anonymous inner classes, a topic covered earlier in the course. You do not need that background to use it. You only need to know that it creates an array and fills it in one expression.

java
Output
cubes first refers to a four-element array, then refers to a new three-element array with values 2, 4, 6. The first array object is no longer referred to by cubes.
Notes on the code
  • The first line uses the new form inside a declaration, creating and filling the array
  • The second line is an assignment to the existing variable cubes, which is the place a bare brace list cannot go
  • new int[] { ... } creates a new array object with the listed values, and the assignment makes cubes refer to it
Best practice

When you need to replace the contents of an existing array variable, use the new operator form rather than the bare brace list.

Choosing Between the Two Notations

Both notations create an array and fill it with values. The difference is where each one can appear. The table below compares them side by side so you can choose the right form for a given statement.

FeatureArray literal (brace list)new operator form
Exampleint[] squares = { 1, 4, 9 };int[] cubes = new int[] { 1, 8, 27 };
Allowed in a declarationYesYes
Allowed in an assignment to an existing variableNoYes
Allowed as a method argument or return valueNo, a bare literal is not allowed in that placeYes, it creates and fills the array in one expression
Typical useDeclare and fill a new array in one lineCreate a new array object and fill it where a literal cannot go

Choosing between the array literal and the new operator form. The rows about method arguments and return values follow from the rule that the new form works in places where a bare literal does not.

Key point

Ask one question: is this statement creating a new variable with its first value, or changing a variable that already exists? A declaration can use the brace list. An assignment to an existing array variable needs the new operator form.

Practice Choosing the Notation

EASY

Assume primes is already declared as an int array. Which statement is valid for replacing its contents with the values 2, 3, 5, 7: primes = { 2, 3, 5, 7 }; or primes = new int[] { 2, 3, 5, 7 };? Then explain your choice in one sentence.

Hints
  • Check whether the statement is a declaration or an assignment to a variable that already exists.
  • The brace list is valid only in a declaration statement.
  • The new operator form creates a new array and can be used in an assignment.

Key Takeaways

  • An array literal, also called a list initializer, is a brace-enclosed list of values that gives initial values to a newly declared array variable, as in int[] squares = { 1, 4, 9, 16, 25, 36, 49 };.
  • After a declaration with an array literal, the variable refers to a newly created array object containing those values; the values belong to the array object, not directly to the variable.
  • The brace list can be used only in a declaration statement. It cannot be used in an assignment to a variable that already existed.
  • The new operator form creates a new array object and fills it with values in one expression, and it can be used in places where a literal cannot go, including assignments.
  • When you need to replace the contents of an existing array variable, use the new operator form rather than the bare brace list.