简体   繁体   English

将最终数组分配给 java 中的数组

[英]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 ?由于 arrays 是引用类型,这是否意味着arr现在是最终的?如果不是,那么当我更改它时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")实际上,如果您将 arr2(final) 等于 arr1(non-final),它不会影响 arr1(non-final),等于操作 (=) 只是表示变量的“值”等于来自内存的引用 (内存中变量值的地址)(因此它只是将变量等同于“值”,而“不”等同于另一个“变量”)

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这意味着如果您的 arr2 是最终的,因此您在设置其值后无法更改其值,但您可以更改 arr1 的值,这将“不”影响 arr2 值

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 . Java 中的final是一个仅适用于变量的概念。

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 ,它既不影响数组也不影响另一个变量。

final disallows re-assignment of the variable, it does not disallow altering the array. final不允许重新分配变量,它不允许更改数组。 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.您会看到两个不同的 arrays,它们都指向同一个实例。 final makes sure that you can not change the arrow going out of finalArr anymore. final确保您不能再更改从finalArr出来的箭头。 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 .但它对您对数组实例本身或对arr所做的操作没有任何限制。


If you are coming from a C/C++ context, final is very different to const in that regard.如果您来自 C/C++ 上下文,那么final在这方面与const非常不同。

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. final int[]不会使数组中的值成为最终值。 In Java arrays are fixed in size but the content can always be changed.在 Java arrays 中,大小固定,但内容始终可以更改。
  • final makes the value of a variable final. final使变量的值最终。 For objects (arrays are objects) the value is the reference to the object that has been created in memory.对于对象(数组是对象),值是对在 memory 中创建的 object 的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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