Concepts / Accessing and Modifying Array Elements

Accessing and Modifying Array Elements

An array type is the base type followed by a pair of empty square brackets, such as int[] or String[].

  • Programming

Why a Declared Array Variable Is Not Yet Useful

Working with an array in Java involves two separate steps that are easy to mix up. The first step gives a variable a type that says it may refer to an array. The second step makes the array itself. This article follows the two steps one at a time, so that you can see what the variable holds after each one.

What do you think happens?

After you write only the declaration int[] A; and nothing else, does the variable A refer to an array?

  • Yes, A now refers to an array of ints

  • No, the declaration only gives A a type and does not create the array

Reveal answer
Answer:

No, the declaration only gives A a type and does not create the array

The source states that declaring a variable gives it a type so that it can refer to an array, but declaring it does not create the array. The array has to be created separately using new.

Building an Array Type From a Base Type

An array type is built from two parts. The first part is the base type of the array, which is the type of each element the array will hold. The second part is a pair of empty square brackets placed after the base type. The brackets mark the type as an array type rather than a single value of the base type.

Pattern
array type = base type + [ ]   (for example, int + [ ] gives int[], and String + [ ] gives String[])

Applying this pattern to the familiar examples gives two types. For an array of ints the type is int[], and for an array of Strings the type is String[]. The base type int or String names what each element is, and the brackets say that the variable's type is an array of that kind of element. Note that int and int[] are different types. A variable of type int is meant for a single int value, so it cannot be the variable that holds an array.

Declaring the Variable That Will Refer to an Array

Once you have the type, a variable is declared by writing the type, then a space, then the variable name, and finally a semicolon. Every Java variable must be declared and must have a type, and an array variable is declared in exactly the same way. The name is what you will later use to reach the array, and the type tells the compiler what kind of thing the variable is allowed to refer to.

java
Notes on the code
  • int[] A; declares a variable named A that is allowed to refer to an array of ints
  • String[] names; declares a variable named names that is allowed to refer to an array of Strings
  • Neither line creates an array. Each line only supplies a type and a name

Creating the Array With new

Creating an array is the step that produces the actual array. In Java this is done with an operator named new. The source notes that arrays in Java are objects and that the special syntax is related to this, but the details of object creation are not needed at this stage. What matters is the pattern you write.

java
Notes on the code
  • int[] A; gives the variable A its type. No array exists yet
  • A = new int[5]; creates an array with base type int and length 5, then assigns it to A
  • The variable A is on the left of the assignment, so it receives the new array as its value
  1. 1
    Read the pattern from left to right. The array variable A is on the left of the assignment, so it will receive the new array as its value.
  2. 2
    On the right, new is followed by the base type int.
  3. 3
    Next comes a length in square brackets, here 5. The length is required, because creating an array means deciding how many elements it has.
  4. 4
    The result is an array of five ints, and that array is what A is assigned to refer to.

The source also says that arrays must be created before they are used. That is why the creation statement has to come before any code that works with the array's elements. A variable that has been declared but not yet assigned an array has nothing to work on.

How the Variable Connects to the Array Object

The statement A = new int[5]; sets up a relationship. The variable A is a name that refers to one array object, and that array holds five elements. The diagram shows only this logical structure: one variable, one array, and a set of five indexed elements. The source does not describe how those elements are stored in memory, so the figure does not claim anything about storage.

Aint[] variableint[] arraycreated by new5 elementsbase type intrefers toholds
After A = new int[5]; runs, how is the variable A connected to the array object, and what does that object hold?

Comparing the Declaration Step With the Creation Step

The two steps often appear on neighboring lines and both mention the same variable name, which makes them easy to confuse. The table separates them by what each step needs and what each step produces.

StepWhat it needsWhat it produces
Declaration, such as int[] A;A base type and a variable nameA variable with an array type, which does not yet hold an array
Creation, such as A = new int[5];A base type and a lengthAn actual array object that the variable refers to

Declaring and creating an array are separate steps

Aint[] variableno array yetnothing assignedAint[] variableint[5] arraycreated by newrefers to
After only the declaration, what does A hold, and how does that change once new creates the array?

Applying the Same Pattern to Another Base Type

The same two-step method works for any base type. The following case was not in the source and uses String as the base type with a length of three.

Worked example

An array of three Strings

Declare a variable named names that can refer to an array of Strings, then create an array of three Strings and assign it to names.

  1. 1
    Write the type

    The base type is String. Adding a pair of empty square brackets gives the array type String[].

  2. 2
    Declare the variable

    Write the type, a space, the name, and a semicolon: String[] names; This gives names a type but creates no array.

  3. 3
    Create the array

    Use new with the base type and a length: new String[3]. The length 3 says the array has three elements.

  4. 4
    Assign it

    Assign the new array to names: names = new String[3];. The variable now refers to the array.

Result

The variable names refers to an array object of three Strings. Declaring it first and creating the array second gave the same two-step structure as the int example.

Mistakes With the Two-Step Pattern

  • Common mistake

    Assuming the declaration already created the array

    What it looks like
    int[] A; followed directly by code that expects A to hold five elements
    Why it fails

    The source states that declaring an array variable gives it a type but does not create the array.

    Do this instead

    Follow the declaration with a creation statement such as A = new int[5]; before using A.

  • Common mistake

    Using the variable before creation

    What it looks like

    Reading or changing the array through A before any new statement has run

    Why it fails

    The source says arrays must be created before use, so a variable with no array yet has nothing to work on.

    Do this instead

    Check that the creation statement comes before any code that works with the array's elements.

  • Common mistake

    Leaving out the length in the creation step

    What it looks like

    Writing new int; or new int[] with no number

    Why it fails

    Creating an array means deciding how many elements it has, and the source says the length is required.

    Do this instead

    Write the base type followed by a length in square brackets, such as new int[5].

  • Common mistake

    Treating int and int[] as the same type

    What it looks like

    Declaring int A; and then assigning new int[5] to A

    Why it fails

    A variable of type int is for a single int value. The brackets are what make a type an array type.

    Do this instead

    Declare the variable with the array type int[] so that it is able to refer to an array.

Practice Setting Up an Array

EASY

Write the declaration and the creation statement for an array of four ints named counts. Write the declaration first, then the statement that creates the array and assigns it to counts. Say what counts refers to after the declaration alone.

Hints
  • The base type is int, so the array type is int[].
  • The creation statement uses new, the base type, and a length of 4 in square brackets.
  • After the declaration alone, counts has a type but does not yet refer to an array.

Key Takeaways

  • An array type is the base type followed by a pair of empty square brackets, such as int[] or String[].
  • Declaring an array variable gives it a type so that it can refer to an array, but the declaration does not create the array.
  • An array is created with the new operator, which requires a base type and a length, as in A = new int[5];.
  • Creation must come before any work on an array's elements, because an array variable must be assigned an array before it is used.