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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With