Question

Possible Duplicate:
Why aren’t arrays expandable?

I am starting to learn Java as my Computer Science school's assignments require this language and I'm liking the language. However, I've seen that Java arrays are not expansible - that is - you must declare their length before using them and it can't be changed further.

I'd like to know exactly why is that? Why the Java language designers chose to to make arrays as such? I guess it's for performance concerns but I'm not sure.

Thanks everyone in advance.

Was it helpful?

Solution

I'd like to know exactly why is that? Why the Java language designers chose to to make arrays as such? I guess it's for performance concerns but I'm not sure.

They designed primitives and arrays to be as simple and low level as possible. They don't do anything special and arrays don't use Object Orientated design at all. i.e. they only have a few useful methods, none specific to arrays.

The idea was that you would write higher level collections such as Lists using these low level constructs.

OTHER TIPS

Java arrays are almost as simple as C arrays. C array is just a allocated memory region of n*m bytes where n is the number of elements in the array and m is the number of bytes needed to store a single element.

Then only thing Java added here is length and probably toString(). All other features can make array performance ineffective. Collections do that very well. Moreover collections are written in java itself that makes them portable.

Why the Java language designers chose to to make arrays as such?

Arrays are one of the programming data structures provided by the language. if you make Array also expandible, it'll become similar to ArrayList.

So, i guess because of two reasons:

  1. To make Java similar to previous languages on basic constructs.
  2. To remove duplication.

Arrays occupy consecutive memory locations and the compiler cannot make sure that the locations following the end of the array are available to be added to the array.

That is why many people Use LinkedList or ArrayList

This question is answred

Why aren't arrays expandable?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top