简体   繁体   中英

Assigning final array to an array in java

Consider the following code:-

int[] arr=new int[8];
final int[] finalArr=arr;

As arrays are reference types,does it mean arr is final now?If not then when i change it what happens to the finalArr ?

Look at the following example:

Actually if you equals arr2(final) to arr1(non-final) it does not affect to arr1(non-final),the equal operation(=) just says that the "value" of a variable equals to a reference from memory(address of value of a variable in memory)(so it just equals a variable to a "value" and "not" to another "variable")

It means that if your arr2 is final so you cannot change its value after you have set its value but you can change the value of arr1 which will "not" affect to arr2 value

static int[] arr1 = new int[]{1, 2, 3};
static final int[] arr2 = arr1;

public static void main(String[] args) {
   System.out.println("before edit| arr1 : " + Arrays.toString(arr1));
   System.out.println("before edit| arr2 : " + Arrays.toString(arr2));

   arr1 = new int[]{5, 2, 4};

   System.out.println("after edit| arr1 : " + Arrays.toString(arr1));
   System.out.println("after edit| arr2 : " + Arrays.toString(arr2));
}

You have to strictly differentiate instances/objects from variables. final in Java is a concept that only applies to variables .

In your code you have two different variables that both refer to the same array instance. One of the variables is final , that neither affects the array nor the other variable at all.

final disallows re-assignment of the variable, it does not disallow altering the array. finalArr[1] = 4; is perfectly valid.

To illustrate this, consider

arr ---------|
             |----> instance created by new int[8]
finalArr ----|

You see two different arrays, both point to the same instance. final makes sure that you can not change the arrow going out of finalArr anymore. So it will always point to that array instance. But it does not give any restrictions regarding what you do to the array instance itself or to arr .


If you are coming from a C/C++ context, final is very different to const in that regard.

Starting from the example.

int[] arr = new int[8];
final int[] finalArr = arr;

// First let's change some values inside the array beeing referenced.
arr[0] = 12;
finalArr[1] = 13;
// arr = {12, 13, 0, 0, 0, 0, 0, 0}
// finalArr = {12, 13, 0, 0, 0, 0, 0, 0}
// This means we can change the values of an final array
// and that changes to arr and finalArr both target the same array.

arr = new int[2];
// arr = {0, 0}
// finalArr = {12, 13, 0, 0, 0, 0, 0, 0}
// arr now points to the new array created. finalArr still references to the old one.

finalArr = new int[2];
// Can't assign finalArr to a new array because it's final.

So to sum it up:

  • final int[] doesn't make the values inside the array final. In Java arrays are fixed in size but the content can always be changed.
  • final makes the value of a variable final. For objects (arrays are objects) the value is the reference to the object that has been created in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM