Assignment Statements and Variables
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 };.
Testing the Belief About Brace Lists
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 that the braces work everywhere an array needs values. This article tests that assumption. 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.
A common belief is that a brace-enclosed list is a general way to hand an array a new set of contents, so that any array variable, new or old, can be given fresh values with it. Before reading the rule, decide for yourself whether that belief holds.
What do you think happens?
Given int[] a = {1, 2, 3}; already on an earlier line, can the statement a = {4, 5, 6}; later replace the contents of a?
Reveal answer
No, the brace list is not allowed in an assignment to an existing variable.
A list initializer can be used only in a declaration statement. It cannot be used in an assignment to a variable that already existed. The rest of this article explains why and shows what to use instead.
Tracing the squares Declaration
Follow the declaration below one step at a time. Keep one question in mind as you go: once the declaration has finished, what does the squares variable refer to, and what does the array contain?
- int[] names the type: an array of int values
- squares is the name of the newly declared variable
- The braces hold a list of seven values separated by commas
- The equals sign assigns the list to the new variable as part of the declaration
- 1The declaration names a new variable squares of type int[].
- 2The brace-enclosed list supplies the seven initial values 1, 4, 9, 16, 25, 36, and 49.
- 3An array object is created that contains those seven values.
- 4The variable squares is set to refer to that array object.
The result is that squares does not hold the values in the sense of one simple number. It refers to the array object the declaration created, and that object contains the seven values. The diagram below shows this relationship after the declaration has finished.
What an Array Literal Does
An array literal, also called a list initializer, is a brace-enclosed list that gives initial values to a newly declared array variable. The list appears inside curly braces with the values separated by commas, and it sits on the same line as the declaration.
The key word is newly declared. The list is attached to the declaration, so it supplies the first contents of a variable at the moment that variable is created. Each value in the list becomes one element of the new array, and the element count follows from the list length. In the squares example, seven values produce an array of seven elements.
Where the Brace List Is Refused
A declaration is the one moment when a variable is being created and given its first value at the same time. An assignment statement works with a variable that already exists, and the list notation is not available there. That is why the same brace-enclosed list that works on the squares declaration would be rejected on a later line that tries to replace the contents of squares.
- Line 1 is a declaration, so the brace list is allowed
- Line 2 is an assignment to an existing variable, so the brace list is not allowed and this line is rejected
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. This form can be used in places where a list initializer cannot, including assignments. The source notes that the syntax looks unusual and resembles the syntax for anonymous inner classes. You do not need that background to use it. You only need to know that it creates and fills an array in one expression.
The source introduces this form with an example that assigns a new value to an existing array variable named cubes of type int[]. The source text is cut off before the exact statement, so the example below is an illustrative version written to match that description.
- Line 1 declares cubes with a list initializer, which is allowed in a declaration
- Line 2 uses the new operator form to create and fill a new array, then assigns it to the existing variable cubes
Replacing the Contents of an Existing Array
The variable cubes already exists and holds three cube values. You want cubes to hold five cube values instead, without using a brace list in an assignment.
- 1Check the statement type
The variable already exists, so this is an assignment, not a declaration. A bare brace list is not allowed here.
- 2Choose the new operator form
Write new int[] followed by the braces and the five values. This creates a new array object and fills it in one expression.
- 3Assign the result
Assign the new array object to cubes. The variable now refers to the new array, which contains the five values.
cubes = new int[] { 1, 8, 27, 64, 125 }; replaces the contents that cubes refers to, using the form the source says is needed when an existing array variable must receive new values.
Choosing Between the Two Notations
The decision comes down to one question: is this statement creating a new variable? If yes, the list initializer is available. If the array variable already exists and you need to give it fresh values, use the new operator form.
| Feature | Array literal (list initializer) | new operator form |
|---|---|---|
| Notation | Brace list such as {1, 2, 3}, attached to a declaration | new int[] followed by braces, such as new int[] {1, 2, 3} |
| Where it can appear | Only in a declaration statement | Other places, including assignments |
| What it does | Gives initial values to a newly declared array variable | Creates a new array object and fills it with values in one expression |
| Use it to replace contents of an existing variable? | No, not allowed | Yes, this is the form to use |
| Example from the source | int[] squares = { 1, 4, 9, 16, 25, 36, 49 }; | Assigning a new array to the existing cubes variable |
Comparing the two notations for creating an array with values.
Mistakes Beginners Make With Brace Lists
Common mistake Using a brace list in an assignment to an existing array variable
What it looks likesquares = { 2, 3, 5, 7 };Why it failsA list initializer can be used only in a declaration statement. squares already exists, so this is an assignment and the brace list is not allowed.
Do this insteadUse the new operator form: squares = new int[] { 2, 3, 5, 7 };Common mistake Assuming the brace list works wherever an array needs values
Why it failsThe brace list is tied to the declaration. Its availability depends on the statement's position, not on whether the values are numbers.
Do this insteadBefore writing braces, check whether the statement declares a new variable.
Common mistake Thinking squares holds the seven values directly
What it looks likeAfter int[] squares = { 1, 4, 9, 16, 25, 36, 49 };, reading squares as if it were a single number
Why it failsThe source says the variable refers to the newly created array object that contains the values.
Do this insteadThink of squares as a reference to an array object, and of the seven values as the contents of that object.
Practice Deciding the Notation
For each statement below, decide whether a bare brace list is allowed, or whether the new operator form is needed. Statement A: int[] primes = { 2, 3, 5, 7 }; Statement B: primes = { 11, 13, 17 }; Statement C: primes = new int[] { 11, 13, 17 };
Hints
- Ask whether the statement creates a new variable or works with one that already exists.
- Statement A is a declaration, so its brace list is allowed.
- Statements B and C both assign to primes, which already exists. Only one of them uses the form the source says to use for replacing contents.
Key Takeaways
- An array literal, also called a list initializer, is a brace-enclosed list that gives initial values to a newly declared array variable, and it can be used only in a declaration statement.
- A declaration is the one moment a variable is created and given its first value, so a brace list is refused 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 other places, including assignments.
- To replace the contents of an existing array variable, use the new operator form rather than the bare brace list.
- After a declaration, the variable refers to the array object that was created, and that object contains the values.