-1

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?

Kishor
  • 325
  • 1
  • 4
  • 6
  • 1
    You should try it and see. – tgdavies Jul 15 '20 at 05:52
  • 3
    If the final variable is a reference, this means that the variable cannot be re-assign to reference another object, but an internal state of the object pointed by that reference variable can be changed i.e. you can add or remove elements from a final array or final collection. – Ashish Karn Jul 15 '20 at 05:56
  • Is this [**link**](https://stackoverflow.com/questions/10339930/final-array-in-java) answer your question: – Ashish Karn Jul 15 '20 at 06:00

3 Answers3

1

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));
}
1

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.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • 1
    We should stick to the official nomenclature. The [Java language Reference](https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html) specifies: "There are two kinds of types in the Java programming language: primitive types (§4.2) and reference types (§4.3)". _final_ applies to both. In effect final primitives are constant, while final references are as you said, they cannot be changed but the data they refer to may be changed. – Jonathan Rosenne Jul 15 '20 at 06:55
0

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.
magicmn
  • 1,787
  • 7
  • 15