• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

size() vs length() vs length

 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Array size is accessed as: myArray.length;

String size is accessed as: myString.length();

ArrayList size is accessed as: myArrayList.size();

I know there are other cases for the various collection classes which I haven't studied in detail yet.

Is there any rationale to these apparent inconsistencies that will help me to remember when to use length, length() or size()? Or do I just have to remember which to use on a case-by-case basis?

Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's one thing that might help you remember ArrayList is size() rather than length. The size() method comes from the Collection interface. And for some collections, length doesn't make sense because it implies a line, which implies an order. Yes, it makes sense for ArrayList, but what about HashSet? Sets don't have an order to them.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:Is there any rationale to these apparent inconsistencies that will help me to remember when to use length, length() or size()? Or do I just have to remember which to use on a case-by-case basis?

First of all, the more you'll practice writing code snippets, the more you'll be used to these differences and after some time you'll remember them without any effort. And in the end you'll even know what to use without having to think about it

Let's start with String and StringBuilder: both respresent a sequence of characters; the first one is immutable, the latter is mutable. For these two it's all about words. And with words it makes (at least for me) more sense to talk about "the length of a word" than "the size of a word". Furthermore, the length (character count) should be accessible for the whole Java Universe, but should be changed only from inside the class. There's no reason someone should be able to change it directly, the length of a word (string) changes if characters are removed and/or added. So if you would have a public length property, you could also change the length of a string directly. So assume you have a StringBuilder with "Java" as value. What would happen if the length was set to 2? Should the value be truncated to "Ja"? Or what if the length was set to 10? Should the value be padded with spaces? That's why good encapsulation matters. Therefore you'll have a (read-only) method to get the character count: the length() method.

Now let's look at ArrayList and all other collections. An ArrayList IS-A Collection. And with collections, it makes again more sense to talk about "the size of a collection" than "the length of a collection". And again, the size (number of elements) should be accessible for the whole Java Universe, but should be changed only from inside the class. Otherwise you'll face similar issues as with String and StringBuilder: if you have an ArrayList with 5 Integers and you could change the size directly to 2 or to 10, what should happen? Therefore you'll have a (read-only) method to get the number of elements in a collection: the size() method.

Last but not least, let's have a look at the array. Arrays are special objects in Java, so each array IS-A Object! They have a simple attribute named length which is public and final. Meaning the property is exposed to the whole world (public), but it's impossible to change and thus read-only (final). There is no "class definition" of an array (you can't find it in any .class file), they're a part of the language itself. Here you'll find the Arrays section in the JLS.

Hope it helps!
Kind regards,
Roel
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matthew and Roel.

Roel - what you say about arrays having a public & final attribute called length is a great way for me to remember. Is this unique to the array type in the Java core language?

Am I ok to think in terms of special case for Arrays (length attribute) and Strings (length() method) whereas all the Collections use size() ? Or will I encounter more special cases? Eg are there special cases amongst the Collections?

Cheers.


 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steffe Wilson wrote:Roel - what you say about arrays having a public & final attribute called length is a great way for me to remember. Is this unique to the array type in the Java core language?

Yes, as I said: each array IS-A Object. That means you can invoke e.g. the getClass() method on any array. And each array has a public final field length. And there is no such thing as an Array class (like String or StringBuilder or ArrayList). So yes, it's unique in the Java core language.

Steffe Wilson wrote:Am I ok to think in terms of special case for Arrays (length attribute) and Strings (length() method) whereas all the Collections use size() ? Or will I encounter more special cases? Eg are there special cases amongst the Collections?

Definitely! No other special cases as far as I can remember (unless the ones you create yourself ). Even Maps which are part of the Collections API but are not a Collection (like Set and List) use the size() method to return the number of elements in the map. But you don't have to worry about the Map interface until the OCPJP exam
 
Steffe Wilson
Ranch Hand
Posts: 165
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great, thanks a lot Roel.
 
nick woodward
Ranch Hand
Posts: 370
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Haven't read the thread, but this helps me remember:

Strings' length() is a method, because Strings have lots of methods.

Arrays don't have lots of methods, so Arrays length is an attribute

Obviously this isn't reasoning, just a method for remembering.

Hope that helps,

Nick
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:Obviously this isn't reasoning, just a method for remembering.

Although it's no reasoning, it's still 100% correct! So it definitely might help to remember the difference between .length and .length().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic