I have a relatively simple question regarding the function of final
in Java.
When I compile this code:
import java.util.*;
import java.lang.*;
import java.io.*;
class Arrays
{
public static void main (String[] args) throws java.lang.Exception
{
final int[] myArray = { 1,2 };
myArray[1] = 3;
System.out.println (myArray[1]);
}
}
The number "3" gets printed. However, I was under the impression that final
meant that the values held in that array (myArray) can no longer change and are constant.
There is also another piece of code here:
final int [] a1 = {1, 2};
int [] b1 = {3, 4};
a1 = b1; System.out.println (a1[1]);
I believe that the system should print "2" since a1
is finalized and the values in the array are constant. However, my friend believes that it should be "4". Please help me clear up my understanding of final
.