5

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.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
Shawn Shroyer
  • 901
  • 1
  • 8
  • 18
  • related: http://stackoverflow.com/q/9297899/697449 – Paul Bellora Mar 04 '12 at 01:43
  • 1
    Vote to reopen. IMO, this is NOT a duplicate of the linked question. This question is why the Java designers didn't **encapsulate** the `length` field. (The linked question is about the API consistency of arrays vs strings ... which is a significantly different issue.) – Stephen C Mar 04 '12 at 01:54
  • 3
    Besides, it is (IMO) and **interesting** question, and worthy of answering. – Stephen C Mar 04 '12 at 02:03
  • @StephenC - I admit I voted for the dupe in part because of the variety of misleading answers here. You have a good point about the intend of the question. – Paul Bellora Mar 04 '12 at 03:56

3 Answers3

1

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.

baba smith
  • 711
  • 7
  • 16
0

That would be easy to implement, and that's why it isn't implemented.

Arrays are simply blocks of storage, with no real controls. They enable an easy and fast storage method for when advanced controls are not required.

You can use things like lists, queues, linked lists, double linked lists etc. which can keep a record of their size, if this is required. Obviously, depending on the language you are using, you may well have to create these yourself.

If you want to have length of array and still use a simple array, you can add 1 to a count variable every time you add something to it and minus one each time you remove something. This will perform the same function as a getLength() method.

DIXONJWDD
  • 1,276
  • 10
  • 20
  • 4
    What? The length *is* stored, it's simply not exposed through a getter method but through a public member. –  Mar 04 '12 at 01:42
-1

Although arrays might be interpreted as objects, they're really just a fixed set of memory addresses containing some other object or variable. In this manner, they have attributes, such as .length, but not necessarily methods, like .getLength()

A lot of information about arrays can be found here: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

brazilianldsjaguar
  • 1,409
  • 1
  • 20
  • 45
  • 2
    This explanation doesn't hold water. In fact, arrays have `Object` as their implicit superclass, and therefore DO have a bunch of method. It follows that the Java designers *could* have defined a `length()` method for all arrays. (The fact that an application program can't add methods is not relevant here.) – Stephen C Mar 04 '12 at 02:02