9

Why is length a data field in when we talk about arrays and length() when we talk about String in Java? Means:

int a[10] = {1,2,3,4,5,6,7,8,9,10};
String str = "foo";
int a_len = a.length;
int str_len = str.length();

Why is length not a function in case of arrays or vice versa?

Vaibhav Agarwal
  • 1,124
  • 4
  • 16
  • 25
  • 3
    likely because the array length is stored as part of the array object, and the string length isn't and would need to be calculated. – Shmiddty Oct 17 '12 at 17:32
  • 1
    @Shmiddty -- Actually, the string length is stored. – Hot Licks Oct 17 '12 at 17:32
  • @Shmiddty: since java's strings are immutable, the value could be cached. – Wug Oct 17 '12 at 17:33
  • 1
    Also discussed here: http://stackoverflow.com/questions/5950155/java-arrays-length – Werner Kvalem Vesterås Oct 17 '12 at 17:33
  • function call needs time, it's not reasonable. why strings don't have such field? – Roman C Oct 17 '12 at 17:33
  • @HotLicks I was just speculating (which is why I didn't post it as an answer). – Shmiddty Oct 17 '12 at 17:35
  • @Shmiddty -- Well, you may be partly true -- it's possible that early on, in version 0.0.1 or whatever, String.length() required computation, and the concept "stuck". But as of versions 1.2 - 5.0, when I was working in the innards of Java, the length of a String object was always stored in the object as a simple int field. – Hot Licks Oct 17 '12 at 17:40
  • 1
    @HotLicks So likely it's related to backwards compatibility? – Shmiddty Oct 17 '12 at 17:40
  • 1
    @Shmiddty -- It's definitely backward compatibility. And maybe a few egos. – Hot Licks Oct 17 '12 at 17:49
  • 1
    Another question is why the same concept is called "size" in other contexts. Lots of inconsistency. – Hot Licks Oct 17 '12 at 17:56
  • @HotLicks That's a nice question too. Do you have an answer for that too? – Vaibhav Agarwal Oct 17 '12 at 17:58
  • 1
    @VaibhavAgarwal -- That I'd ascribe to carelessness early on. – Hot Licks Oct 17 '12 at 18:18
  • *Everybody* here is speculating. The answer is "because that's the way Gosling *et al.* designed it". Not much point in asking here: you will get guesswork and if you're lucky some valid *post hoc* reasoning. You're asking in the wrong place. Not constructive. – user207421 Oct 17 '12 at 23:09

3 Answers3

9

Simply: that's just the way it is, and the way it's always been.

It's specified in JLS section 10.7 that arrays have a public final length field. It could have been specified as a method instead, for consistency - but it just wasn't... equally String could have made an implementation decision to have a public final length field - but again, it happens not to be that way.

There are a few bits of inconsistency which have survived since 1.0 - obviously these things really can't be changed after release...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • And since the JIT will likely inline `String:length()` anyway, there shouldn't be any difference in performance either. – cdhowie Oct 17 '12 at 17:35
  • 1
    Yep, I'm sure if it were done over it would be done differently. Either make array length a method call or introduce a general scheme for read-only fields for all objects. – Hot Licks Oct 17 '12 at 17:36
  • @jon skeet: So, didn't it occur in the next releases means they could have just added length data field to String? – Vaibhav Agarwal Oct 17 '12 at 17:39
  • 1
    @VaibhavAgarwal: That would be moving away from the generally accepted best practice of separating API from implementation. The better fix would have been to introduce a `length()` method in arrays, but it would have been confusing to have both, IMO. – Jon Skeet Oct 17 '12 at 17:40
  • Ok. So i take is as this was the first implementation they came up with and didn't want to change it later. Right? – Vaibhav Agarwal Oct 17 '12 at 17:44
  • @VaibhavAgarwal -- Yep. And there are lots of things in Java (and most other languages) like that. Java in particular has been quite rigid on it's upward compatibility points, and so many "reasonable" enhancements/changes have been rejected. – Hot Licks Oct 17 '12 at 17:46
  • @Hot Licks Point accepted. But the thing I am confused about is that if we want to keep a language upward compatible then why do we have obsolete data types? Why not keep them? – Vaibhav Agarwal Oct 17 '12 at 17:49
  • 1
    First off, Java has only very rarely (if ever) completely eliminated a class or method -- generally they're just "deprecated" and strongly discouraged. Secondly, the guardians of the API (and especially the API outside of the main `java.` classes) are different from the guardians of the Java Virtual Machine, bytecodes, et al. The latter group is (usually) conservative to a fault, while the former group is often far too quick to throw in new features and options. – Hot Licks Oct 17 '12 at 17:54
4

String implements CharSequence and thus length needs to be a method in this case since interfaces can only specify methods.

Arrays don't need to implement any interface, so length is implemented as a public final field in this case to avoid extra method call overhead.

Edit:

As Hot Licks pointed out below, CharSequence didn't exist before Java 1.4. Although CharSequence obviously wasn't the driving reason behind this design choice (since it didn't exist), it would still make sense to choose this design with the idea that String might need to implement new interfaces in the future.

DaoWen
  • 32,589
  • 6
  • 74
  • 101
  • 3
    There was no such thing as `CharSequence` when this decision was made. – Hot Licks Oct 17 '12 at 17:40
  • @HotLicks - That's a really good point, and it's evidenced by the 1.2 docs for [String](http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/jdk1.2/api/java/lang/String.html). Although CharSequence didn't exist at the time, the ability to use `length` in a future interface could very well have played a role in the decision. I'll update my answer though. – DaoWen Oct 17 '12 at 17:45
  • 1
    A bigger question is why Java didn't implement a base class for String-like objects and make String a subclass. CharSequence is a weak attempt to recover from this oversight. – Hot Licks Oct 17 '12 at 17:48
0

It's considered a variable and not a method. It would be as if you made a class and said Public static int num and later in a method told that num to be equal to something globally

Gene Parmesan
  • 102
  • 2
  • 12