Skip to main content
2 of 7
added 131 characters in body
CPlus
  • 1.2k
  • 1
  • 10
  • 26

Why does Java not have 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 why is there 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 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?

This question is not opinion-based. I am asking what the reasons are, not how important they are or if they are relevant.

CPlus
  • 1.2k
  • 1
  • 10
  • 26