Why won't declaring an array final make it imm

2019-03-18 06:50发布

Why won't declaring an array final make it immutable in Java? Doesn't declaring something final mean it can't be changed?

From question related to immutable array it's clear that declaring an array final doesn't make it unchangeable.

The following is possible.

final int[] array = new int[] {0, 1, 2, 3};
array[0] = 42;

My question is: What is the function of declaring final here then?

6条回答
不美不萌又怎样
2楼-- · 2019-03-18 07:39

final is only about the reference that is marked by it; there is no such thing as an immutable array in Java. When you say

private final int[] xs = new int[20];

you won't be allowed to say

xs = new int[10];

later on. That's all what final is about. More generally, ensuring that an object is immutable is often hard work and full of a range of subtleties. The language itself doesn't give much to this end out of the box.

查看更多
神经病院院长
3楼-- · 2019-03-18 07:39

Like everyone commented, declaring it final will not let you assign another array to your variable. Read more here

The array itself will retain it's properties same as before.

查看更多
看我几分像从前
4楼-- · 2019-03-18 07:40

Here you are making a object reference final, not a primitive. (Arrays are special Java objects, too.) Making a reference final means that you cannot make it refer something else once it is initialized. But of course you can change the state of an object referred by a final variable.

查看更多
霸刀☆藐视天下
5楼-- · 2019-03-18 07:40

declaring an array

In Java, we declare variables, and create objects (such as arrays). These actions are independent; we can declare a variable without creating an object, and create an object without declaring a variable.

Immutability is a property of an object, while final is a property of a variable. An object may be referred to by several variables. Which variable should govern immutability of the array object?

int[] a = {1,2,3};
final int[] b = a;
a[0] = 10; // should this be legal?

If we permit this, the supposedly immutable value of b[0] has been changed. But how can the compiler prevent this? By making it impossible to assign a non-final to a final reference? How would I initialize the array, then? I couldn't do so in a loop ...

And: What does it even mean for an array to be immutable? For instance, should the following compile?

final int[][] a = {{1,2}, {3,4}};
a[1][1] = 5;

C++ solves this by permitting const to be specified (or omitted) for each level of indirection. In Java, final can be specified (or omitted) once per variable. Yes, final is a simpler const, much like Java is a simpler C++. Let's keep it simple, shall we?

查看更多
劳资没心,怎么记你
6楼-- · 2019-03-18 07:47

final means you can't change the reference - ie you can't assign another array to that field.

"Immutable" means you can't change the contents of the array - final has no effect on that.

As your code shows, you can assign a value to one of the elements of the array, which doesn't change the reference to the array

查看更多
地球回转人心会变
7楼-- · 2019-03-18 07:51

It means that a new instance of the object would not be allowed.

查看更多
登录 后发表回答