Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an array's length immutable? [duplicate]

Tags:

java

arrays

Why is an array's length immutable (at least in java)? I know next to nothing about the inner workings of a programming language, so to me it seems easy to make an array's length changeable. I assume that there is a good reason for array lengths being immutable, probably related to performance. Does anybody know this reason?

Sorry if this question has been asked before. If it has, I didn't find it in about 10 minutes of searching.

EDIT: I know that when an array is is initialized, a certain amount of memory is allocated. Why can't more memory be allocated?

like image 786
The Guy with The Hat Avatar asked Nov 15 '13 18:11

The Guy with The Hat


People also ask

Why is array length not a method?

Since arrays are fixed length defined at the time they are instantiated length is a public final field on the class. There is no need to make it a method since there is no calculation to be done at run time.

Why are arrays immutable in Java?

A Mutable object means the object state or values can be changed, while an immutable object means the object values are fixed. Arrays in Java are mutable because you can still change the values of the array after it has been created.

Can we change an array's size at runtime?

Size of an array If you create an array by initializing its values directly, the size will be the number of elements in it. Thus the size of the array is determined at the time of its creation or, initialization once it is done you cannot change the size of the array.

Why does array length have no parentheses?

The length property makes finding the array as easy as calling . length on your array. There are no parentheses as this is a property, not a function, on the array. The length is one based meaning in the example below our length would return 3 but the indexes in the array would be 0, 1, and 2.


2 Answers

The essence of this problem lies in how language designers chose to partition up the tasks that a language needs to perform, and assign those to different constructs.

In Java, the standard array type has a fixed length, because it's intended to be a low-level, bare-bones implementation, that the developer can build on top of.

In contrast, if you want a more fully featured array, check out the ArrayList<> class. The idea is to give you, the developer, a wide range of choices for your tools, so you can always pick something appropriate for the job.

When talking about arrays, the machinery necessary to allow for variable-length arrays is nontrivial. So the creators of Java chose to put this functionality in the ArrayList<> class, rather than making it mandatory overhead for anyone who just wants a simple array.

Why is lengthening an array nontrivial? Computer memory is finite, so we have to store objects next to each other. When you want to lengthen your array, what if there's already another object in the nearby memory addresses?

In this case, there isn't enough space to lengthen the array in-place, so one has to move data around to make room. Usually this is done by finding an larger, empty block of memory, and copying the array over while lengthening it. The mathematics of how to do this in the best way possible (e.g. how much empty space to leave?) are interesting and nontrivial. ArrayList<> abstracts this process for you, so you don't have to handle it yourself, and you can be confident that the designers of the ArrayList<> class chose an implementation that will perform well (on average) for your application.

like image 192
Max Wallace Avatar answered Oct 08 '22 09:10

Max Wallace


Nothing to do with performance. The length is the size which was allocated. You can't change what you have already set when you created it. The size is the amount of space actually allocated and you can't change this either.

What you can do is take a copy of the array with a different size.

like image 43
Peter Lawrey Avatar answered Oct 08 '22 07:10

Peter Lawrey