9

Possible Duplicate:
count vs length vs size in a collection

In Java in particular, on Strings, you call string.length(), whereas in Lists you call list.size(). Is there a technical difference between the two terms, seeing as a String is really just a list of chars?

Any comments appreciated.

Community
  • 1
  • 1
Tom R
  • 5,991
  • 9
  • 35
  • 41

6 Answers6

14

In general, length() is used when something has a constant length, while size() is used on something with a variable length. Past that, I know of no good reason for using two nearly-synonymous terms.

Joel Rondeau
  • 7,486
  • 2
  • 42
  • 54
  • 1
    Assuming this, length() is for ***immutable*** data types like *String* and size() is for ***mutable*** data types like *List*. – Alex Aug 03 '11 at 21:54
9

Ideally, count would be the number of items, and size would be the amount of storage taken up (as in sizeof).

In practice, all three (including length, which is the most ambiguous) are muddled up in many widely-used libraries, so there's no point trying to impose a pattern on them at this stage.

Daniel Earwicker
  • 114,894
  • 38
  • 205
  • 284
  • 2
    +1 for admitting that programmers aren't perfect. – Tom R Feb 04 '10 at 22:04
  • 2
    Sometimes `size` is used for the number of items, and `capacity` is for the amount of storage taken up. – Johannes Schaub - litb Feb 04 '10 at 22:19
  • @litb, regarding C++, isn't it possible that `vector::capacity` could return a non-zero value although no storage has yet been allocated? It would be undefined behaviour to attempt to use the storage before a proper `resize` happens. So an implementation could delay actual allocation until then - and then it might also allocate more than we asked for. So the `capacity` is more like a hint to the implementation than an actual statement of storage use. No doubt such an implementation would break quite a few extant programs, those that do a `v.reserve(200)` and then go straight for `&v[100]`. – Daniel Earwicker Feb 05 '10 at 09:38
  • such behavior would invalidate `.begin()` and `.end()`, i think. However you are guaranteed that they stay valid for at least `.capacity() - .size()` push-backs. – Johannes Schaub - litb Feb 06 '10 at 20:18
4

For me,

"length" implies an order, you are measuring a length from the start to the end.

"size" implies how big something is without implying an order, start or end.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
3

I don't think so. It is more likely caused by the API being work on a large team and different people have different take on what the name should be, especially with words that synonym in certain context, such as size and length in this matter.

DJ.
  • 6,664
  • 1
  • 33
  • 48
2

As others have touched on, I think it is a semantic difference. Arrays are very clearly defined and are referred to as having length. A collection of objects is more fuzzy, partly because its implementation (is it an array, linked-list, tree?) isn't necessarily your concern. A tree doesn't really have a length, but it has a definite size, so size might make more sense semantically.

Jonathon Faust
  • 12,396
  • 4
  • 50
  • 63
  • Not using `size` for arrays makes sense as arrays do have a size but it's not the number of elements. An `int[5]` array has a length of 5 and a size of 20 (5 elements and each is 4 bytes, make 20 bytes). But this collides with your tree example, as if tree size is the number of elements in the tree, then how would you name the actual size that in memory? Hence I would not use size for trees either but `count` or `numberOfNodes`. I avoid `length` in general, unless we talk of things that have a length by definition, e.g. a String has a length (and a size!) but it should not have a `count`. – Mecki Oct 27 '17 at 11:12
0

To me, "size" is used when describing a structure or other way to organize bytes in a computer. It doesn't necessarily have to be instantiated. "length", on the other hand, implies how large a particular buffer of bytes in memory is at a given point.

E.g. What is the length of the buffer you're providing? It is the same number of bytes as the size of structure x.

jdizzle
  • 4,078
  • 1
  • 30
  • 38