7

Possible Duplicate:
Java - Array’s length property
String.length() vs Array.length

I'm currently in my AP Computer Science class in high school and I came across this in my reading.

From what I understand, .length() is a method used for strings, but why isn't .length() a method when applied on arrays? I understand that they're different objects, but why didn't Java just make another method for finding the length of arrays?

I appreciate any response I get. Thanks!

Community
  • 1
  • 1
joejacobz
  • 195
  • 1
  • 7
  • possible duplicate of [String.length() vs Array.length](http://stackoverflow.com/questions/12082196/string-length-vs-array-length), also http://stackoverflow.com/questions/8720220/why-is-string-length-a-method – Thilo Jan 28 '13 at 00:00
  • `length` is a member of the array. About why it is member and not a method (in which case you have to refer to it as `length()`), I will leave it for other people to answer. – nhahtdh Jan 28 '13 at 00:00
  • (This is a dupe, but I'm not going to spend the time finding the prior several questions.) That's just the way it is. At the time it probably seemed to make sense to make `length` a pseudo-field instead of a pseudo method. – Hot Licks Jan 28 '13 at 00:00
  • related: http://stackoverflow.com/questions/9297899/java-arrays-length-property – Paul Bellora Jan 28 '13 at 00:02
  • Sorry for the dupe, but I appreciate the answers! – joejacobz Jan 28 '13 at 00:03

3 Answers3

5

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.

See this section of the Java Spec for details: http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7

Now, as for the design question of why they didn't provide an accessor method to obtain the value isn't specified. Perhaps this was done before any other convention was set and this is just a legacy thing. Only the language designers would know the "why" portion of their decision to do it this way.

Michael
  • 2,460
  • 3
  • 27
  • 47
2

Arrays are defined in the Java Language Specification #10.7. In particular:

The members of an array type are all of the following:

  • The public final field length, which contains the number of components of the array. length may be positive or zero.
  • [...]

I can't answer why this approach was chosen by the language designers.

Interestingly, it was already the case in the Oak specifications, which is the ancestor of Java.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
  • I've always wondered why `getClass()` isn't the `class` data member instead. – Hot Licks Jan 28 '13 at 00:02
  • 1
    @HotLicks: that might interfere with subclassing. After all, it needs to be dispatched (like a method) at runtime to the actual (not the declared) class. – Thilo Jan 28 '13 at 00:03
  • 3
    Under the hood, getting the length "field" of an array is actually using the 'arraylength' bytecode instruction. For getClass(), this is an actual virtual method call. – Ilya Jan 28 '13 at 00:05
  • @Ilya - The class of an object is essentially a field in the object, just like the length of an array. The fact that there is an `arraylength` bytecode is something of an anomaly -- the wart beneath the wart that is `.length`. – Hot Licks Jan 28 '13 at 00:08
  • @Ilya Even if it had been a method, it is called often enough that it would have been inlined where it is important anyway (not in Java 1.0 though!). – assylias Jan 28 '13 at 00:09
  • 2
    @Thilo - Why would it interfere with subclassing? The class is a final field in Object. – Hot Licks Jan 28 '13 at 00:10
  • That's interesting. So how does `super.getClass()` work? It gets called on the same object, after all. – Thilo Jan 28 '13 at 00:12
  • 1
    @Thilo `super.getClass()` would return the same `Class`, since `getClass()` returns the runtime type. But I think you're confused - `getClass` is final and doesn't get overridden. – Paul Bellora Jan 28 '13 at 01:15
  • 1
    @HotLicks and Paul: okay, i was confused. But the problem remains that a .class data member would conflict with the static .class that is already there (like `String.class`). – Thilo Jan 28 '13 at 01:51
  • @Thilo - `String.class` is a fiction -- the compiler translates it into a LDC of the class. If it were a class data member it would be String.class.class, but why have a data member that points at itself? – Hot Licks Jan 28 '13 at 04:25
  • the Oak specification link has gone AWOL – Rob Sep 07 '17 at 08:00
  • 1
    @Rob found another one, see update. – assylias Sep 07 '17 at 10:37
1

I doubt that there's a good technical reason for this.

I suspect that this is one of those little inconsistencies that didn't get spotted early enough to get fixed without breaking a ton of code.

NPE
  • 486,780
  • 108
  • 951
  • 1,012