Skip to main content
5 of 7
Made more specific that I am not soliciting opinions
CPlus
  • 1.2k
  • 1
  • 10
  • 26

What prevents Java from having immutable primitive arrays?

Java never had immutable primitive arrays. However Java does have an immutable List or Map or other collection classes and of course final primitive fields and variables. In Java if you try to make an Object or array final you only make the reference final. The reference will only point to the same actual object but this does not make the underlying object immutable.

If you can make Java primitives final and references to objects and arrays final and make fields of objects final is there a particular reason for there being no syntax to make arrays immutable? An easy way to implement such syntax could be:

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

One advantage I see would be not needing to create defensive copies because the array could be immutable anyway. What are the specific less obvious implications to immutable arrays that caused them to be left out of Java and why do they apply to arrays specifically but not primitives? What language or implementation mechanics contradict with immutable arrays and how so?

CPlus
  • 1.2k
  • 1
  • 10
  • 26