Academic Java    Java Tutorial  >  Arrays  >  array assignment (a)

array assignment Example

When one array is assigned to another it is the reference to the array that is assigned.

This means that modifications of the array assigned to will be reflected in the array that was assigned.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Array Assignment Example
class ArrayAssignmentExample {

   public static void main(String[] args) {

      int[] ia = {8,3,9};

      int[] ua = ia;

      ua[1] = 17;

   }

}

6 An array is initialized.

8 The array ia is assigned to array ua.
In fact the value that is assigned, that is the value of ia, is a reference to the array.
So the assignment ua = ia means that ua now refers to the same array as ia.

10 The literal value 17 is assigned to ua[1].
Because ua is the same array as ia the assignment is also to ia.