Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a technical difference between the terms "length" and "size" (in programming, of course)? [duplicate]

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.

like image 814
Tom R Avatar asked Feb 04 '10 21:02

Tom R


2 Answers

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.

like image 104
Joel Rondeau Avatar answered Nov 15 '22 15:11

Joel Rondeau


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.

like image 33
Daniel Earwicker Avatar answered Nov 15 '22 15:11

Daniel Earwicker