Sub topic 5
References, Default Values, and Element Types
An array in Java is an object: a piece of data created in memory. An array variable holds a reference, which is a way to refer to that object. It does not hold the whole array itself.
This matters because two variables can refer to the same array. A change made through one variable is visible through the other.
Sharing an array
An element is one position in an array. The element type says what kind of value each position can hold. For example, an int[] holds int values.
When you assign one array variable to another, Java copies the reference, not the array. Both variables then refer to the same array:
Reassigning one variable later does not change the other variable. It only changes which array that variable refers to.
Default values
When you create an array with new, Java gives each element a default value. The value depends on the element type.
| Element type | Default value |
|---|---|
| `byte`, `short`, `int`, or `long` | `0` |
| `float` or `double` | `0.0` |
| `boolean` | `false` |
| `char` | The character with code point zero, not the digit `0` |
| Any reference type | `null` |
A reference type is a type whose values refer to objects. For example, String is a reference type. The value null means a reference does not refer to an object.
Creating a String array fills its positions with null. It does not create a string for each position.
Primitive elements and object references
A primitive type is a basic Java type such as int or boolean. An array of a primitive type stores those values in its elements.
An array of a reference type stores references in its elements. Each reference can point to an object or be null. Two references can point to the same object, just as two array variables can point to the same array.
The following program shows both kinds of sharing. A class is a blueprint for creating objects. The Counter class has a field named value; a field is a variable belonging to an object.
Save the program as ArrayReferences.java:
public class ArrayReferences {
static class Counter {
int value;
}
public static void main(String[] args) {
int[] scores = new int[2];
int[] alias = scores;
System.out.println(scores[0]);
alias[0] = 7;
System.out.println(scores[0]);
alias = new int[2];
alias[0] = 9;
System.out.println(scores[0]);
Counter[] counters = new Counter[2];
System.out.println(counters[0]);
counters[0] = new Counter();
counters[0].value = 5;
Counter first = counters[0];
first.value = 8;
System.out.println(counters[0].value);
}
}Compile and run it:
javac ArrayReferences.java
java ArrayReferencesThe program prints 0, 7, 7, null, and 8, each on a separate line.
The first 0 is the default value of an int element. Changing alias[0] also changes what scores[0] reads, because both variables refer to the same array. After alias is assigned a new array, changing it no longer affects scores.
The Counter array starts with null elements. After a Counter is created, both first and counters[0] refer to it. Changing its field through first is visible through counters[0].
Trying to read a field through null causes a NullPointerException, a runtime error. For example, counters[0].value would fail before the program assigns a Counter to that element.
Check your understanding
Exercise
A new int array has two elements. You assign its reference to a second variable, then use that variable to set element 0 to 4. What does element 0 contain when read through the first variable?
Show answer
It contains 4. Both variables refer to the same array.
Exercise
What does an element of a newly created String array contain before you assign it a string?
Show answer
It contains null, not an empty string.