1

Java has immutable strings so its initialised size never changes (like size of an array). The question is - if size never changes why Java requires to call a method length() rather than property length like in an array? Is length()

Filip Bartuzi
  • 5,711
  • 7
  • 54
  • 102
  • Because that's the way it was defined. The bigger question is why arrays have the `length` read-only property, vs a `length()` method, since they are "just objects", like Strings. – Hot Licks Nov 09 '14 at 13:00
  • Another identical question is this one: http://stackoverflow.com/questions/14856327/why-is-string-length-a-method-and-int-length-a-property – vefthym Nov 09 '14 at 13:00
  • 2
    Not every thing must have a plausible reason. The java designers may never have thought about it, or was under the influence of moon phase when he designed one API in a different way. – Siyuan Ren Nov 09 '14 at 13:02
  • 2
    @SiyuanRen - Yeah, a lot of decisions went into the design of the original Java. A bunch of them have been shown to be less than ideal, but that's always going to be the case. – Hot Licks Nov 09 '14 at 13:03
  • I feel like you're assuming that getter method is better than property reading. Why is that? It's like unnecessary method wrapping – Filip Bartuzi Nov 09 '14 at 13:07
  • @FilipBartuzi read about polymorphism, encapsulation, object-oriented design. Fields are not polymorphic in Java. – JB Nizet Nov 09 '14 at 13:11
  • Ok, these are my thoughts now (let me know if I'm wrong @JBNizet): Because `length()` is a getter method we could create our own class (`MyString`) that extends `String` and reuse this method to read length. However I assume that initialiser sets up property of length so subclass (`MyString`) would have access to it without gatherer anyway. Am I right? – Filip Bartuzi Nov 09 '14 at 13:22
  • No, you can't because String is final. But, as the answer of Thiago explains, a String is also a CharSequence, and you can get the length of a String by referring to it as a CharSequence. This wouldn't be possible if length was a field. – JB Nizet Nov 09 '14 at 13:27
  • @JBNizet - But CharSequence didn't exist when `length()` was decided upon. – Hot Licks Nov 09 '14 at 20:15
  • @HotLicks so how did it work before? – Filip Bartuzi Nov 09 '14 at 20:48
  • @HotLicks the point is that CharSequence wouldn't be possible if length was a field, and not a method. Methods are what allows polymorphism. – JB Nizet Nov 09 '14 at 20:50
  • @JBNizet - There are lots of things that aren't possible in Java, or which are only accomplished by distorting the original design. – Hot Licks Nov 09 '14 at 22:07
  • @FilipBartuzi - There was no CharSequence. It was an afterthought. A better design would have had String be a subclass of a common class that also had a MutableString subclass or some such, but that didn't happen. – Hot Licks Nov 09 '14 at 22:09
  • @HotLicks my point is: the original design (using a method rather than a field) was right: it allows polymorphism and encapsulation, which a field doesn't allow. – JB Nizet Nov 09 '14 at 22:11

1 Answers1

3

Because length() is defined in CharSequence interface, which is implemented by String. Interfaces can't define properties in Java. Also, this makes it more interchangeable with other types.

In many methods, you may rely only on CharSequence interface instead of a real String, then you can use an instance of CharBuffer, Segment, String, StringBuffer or StringBuilder in the call site.

See also: CharSequence documentation.

Thiago Negri
  • 5,221
  • 2
  • 28
  • 39