Sub topic 1
What an Array Is
An array is a Java object that holds a fixed number of values in a set order. An object is an item created by a program that can hold data. Each value in an array is called an element.
For example, an array can hold three test scores:
| Position | Element |
|---|---|
| 0 | 80 |
| 1 | 90 |
| 2 | 100 |
The positions start at 0, not 1. The number used to identify a position is called an index. In this array, the score 80 is at index 0, and the score 100 is at index 2.
Arrays are useful when several values belong together. A program might keep daily temperatures, game scores, or the names of people on a team in an array. Instead of managing a separate variable for each value, the program can use one array.
A variable is a named place for a value. When a variable refers to an array, it gives the program a way to work with that array.
One array, many elements
Think of an array as a row of numbered slots. Each slot holds one element. The slots have a set order, even if some elements have the same value.
Here is a model of the scores array:
The three boxes on the right show slots in the same array. They do not show three separate arrays.
An array can have repeated values. For example, the scores at positions 0 and 1 could both be 80. They would still be two elements in two different slots.
The order of elements does not mean that Java has sorted them. If the scores are 90, 80, and 100, that is the order the array holds.
Fixed size, changeable contents
An array gets its size when it is created. The size is the number of slots in the array. It cannot grow or shrink later.
A three-slot array always has three slots. But you can change a value in one of those slots. For example, you can replace the first score, 80, with 85. The array still has three elements.
Java arrays can also have zero slots. A zero-length array is a real array; it just has no elements.
A variable can later refer to a different array. That does not change the size of the first array. It means the variable now gives the program access to another array.
Element types
A type tells Java what kind of value can be used in a place in a program. Every slot in an array has the same element type.
For example, an array whose element type is int holds int values. An int is a Java type for integers, such as -3, 0, and 80. An integer has no fractional part.
Here are two examples of array types:
| Array type | What its slots hold |
|---|---|
| int[] | int values |
| String[] | References to String objects, or null |
A String is a Java object that represents text. A reference is a value that lets a program reach an object. The special value null means that a reference does not refer to an object.
An int is a primitive type, which means its values are not objects. In an int array, the slots hold int values. In a String array, the slots hold references to String objects, or null.
The element type helps Java check what goes into an array. For example, you cannot put the text value "hello" into an int slot.
The array itself is an object in both cases. An int array is an object even though the int values inside it are not objects.
Worked example: test scores
This program creates an array of three scores. It prints the number of slots, prints the first score, changes that score, and prints it again.
Save the code in a file named ArrayExample.java:
public class ArrayExample {
public static void main(String[] args) {
int[] scores = {80, 90, 100};
System.out.println(scores.length);
System.out.println(scores[0]);
scores[0] = 85;
System.out.println(scores[0]);
}
}A class is a container for Java code. In this program, the class is named ArrayExample. A method is a named block of code that performs a task. Java starts this program by running its main method.
The part named String[] args lets Java pass extra text to the program when it starts. This example does not use that text.
The line beginning with int[] creates the scores array:
- int[] says that scores refers to an array of int values.
- The values between the braces, 80, 90, and 100, are the starting elements.
- Because there are three starting elements, the array has three slots.
The expression scores.length gives the number of slots. The expression scores[0] refers to the element at index 0. The line with an equals sign changes that element to 85.
System.out.println prints a value on its own line.
To run the program, you need the Java Development Kit, or JDK. The JDK includes the tools used to prepare and run Java programs. In a terminal, a text-based window for entering commands, go to the folder containing ArrayExample.java. Then run:
javac ArrayExample.java
java ArrayExampleThe first command prepares the program to run. The second runs it. The output is:
3
80
85The first line is 3 because the array has three slots. The second line is 80 because that was the first score. The last line is 85 because the program changed that score. The array still has three slots after the change.
What an array does not do
An array keeps values in positions, but it does not explain what the values mean. In the example, the program treats the numbers as scores because that is how the programmer chose to use them. The array itself does not know whether 80 is a test score, a temperature, or something else.
An array also does not add up its values or put them in order by itself. The program must ask Java to do those tasks.
Finally, an array does not create a separate name for each element. The elements share the array name and are identified by their positions. This is helpful when a program has many related values to manage.
Check your understanding
Exercise
An array has four slots. Can its first and second slots both hold the value 7?
Show answer
Yes. Equal values can appear in different slots.
Exercise
A three-slot array contains 80, 90, and 100. If 80 is replaced with 85, how many slots does the array have?
Show answer
It still has three slots. Changing an element does not change the array's size.
Exercise
What is the difference between an int array and a String array?
Show answer
An int array holds int values. A String array holds references to String objects, or null.