Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length in Arrays

Tags:

java

arrays

Just a simple question, but why is length in an array not encapsulated? Is this just for beginners in programming because I feel like a getLength() method would be easy to implement.

like image 423
Shawn Shroyer Avatar asked Nov 26 '22 02:11

Shawn Shroyer


1 Answers

I've an assumption but I cant say that this is the reason why the designers of java did it that way.

An array is a container (and an Object of course) with a fixed length. And its a very basic one, so encapsulation in this case would be nice but not critical. Take a look at ArrayList for example, which can be considered as an encapsulation of an array.

A basic array has many benefits over other types that encapsulates it. And so maybe when the first version of Java came out it lacked most of the optimization that it has today. So having a data structure with the simplicity (in term of performance) of an array is a good thing.

Also it could be as simple as they didnt think about that at that time and afterwards they had to maintain a backwards compatibility :)

Anyway, if you find interesting stuff about that (maybe you could email sun/oracle) please post! Thats a nice question.

like image 62
baba smith Avatar answered Dec 14 '22 23:12

baba smith