What is the difference between .size()
and .length
? Is .size()
only for arraylists and .length
only for arrays?

- 887
- 1
- 10
- 19
-
This seems programming language specific, and there's no tag. – SBI Nov 25 '13 at 12:17
-
The canonical answer: http://stackoverflow.com/questions/300522/count-vs-length-vs-size-in-a-collection This one is also good: http://stackoverflow.com/questions/1965500/length-and-length-in-java – RedGreenCode Feb 14 '15 at 02:39
6 Answers
size()
is a method specified in java.util.Collection
, which is then inherited by every data structure in the standard library. length
is a field on any array (arrays are objects, you just don't see the class normally), and length()
is a method on java.lang.String
, which is just a thin wrapper on a char[]
anyway.
Perhaps by design, Strings are immutable, and all of the top-level Collection subclasses are mutable. So where you see "length" you know that's constant, and where you see "size" it isn't.

- 2,169
- 3
- 17
- 30

- 2,927
- 2
- 17
- 23
-
10"which is just a thin wrapper on a String[] anyway". did you mean `char[]`? – Nick Louloudakis Feb 23 '16 at 10:45
-
I know this thread is mad old, but I think Matt is right and the difference has to do with how they are stored in memory. Array elements are stored sequentially, and the way you access them is `base + offset` where `base` is the start (index 0); this also makes arrays faster, relatively speaking. ArrayLists are in the linked list family, and are not stored sequentially, which is why they are "resizable." So, even though conceptually, linked lists have a "length," they are not stored in memory as a "list" and so they use the more generic "size" instead. – David Mordigal Dec 17 '16 at 23:39
-
-
@MattPuntam: as you say and Java also; **size() is a method specified in java.util.Collection,** but I didn't get where length is defined. in Array. it's my interview question. can you please demonstrate it to clarify mine douts? – Onic Team Jul 09 '17 at 18:16
length is constant which is used to find out the array storing capacity not the number of elements in the array
Example:
int[] a = new int[5]
a.length
always returns 5, which is called the capacity of an array. But
number of elements in the array is called size
Example:
int[] a = new int[5]
a[0] = 10
Here the size would be 1, but a.length
is still 5. Mind that there is no actual property or method called size
on an array so you can't just call a.size
or a.size()
to get the value 1.
The size()
method is available for collections, length
works with arrays in Java.

- 16,842
- 17
- 45
- 54

- 625
- 5
- 7
-
9You cannot invoke `size()` for a primitive array such as `int[]`. Thus `a.size=1` doesn't make sense. – Chinmay jain Sep 06 '17 at 11:07
-
See https://stackoverflow.com/questions/46073467/java-array-attribute-size for a discussion of this answer. – NPE Sep 06 '17 at 11:10
-
@Chinmayjain: that wasn't an assignment; that was just saying what the result would be. – JESii Jan 18 '18 at 16:14
-
1@JESii even if it's not assignment, it's _still_ wrong. There's simply no such thing as `a.size`. It just doesn't exist, so to claim that it "would be" 1 is meaningless. – xdavidliu Mar 26 '20 at 20:49
.length
is a field, containing the capacity (NOT the number of elements the array contains at the moment) of arrays.length()
is a method used by Strings (amongst others), it returns the number of chars in the String; with Strings, capacity and number of containing elements (chars) have the same value.size()
is a method implemented by all members of Collection (lists, sets, stacks,...). It returns the number of elements (NOT the capacity; some collections even don´t have a defined capacity) the collection contains.

- 103
- 1
- 5
length variable:
In Java, array (not java.util.Array) is a predefined class in the language itself. To find the elements of an array, designers used length variable (length is a field member in the predefined class). They must have given length() itself to have uniformity in Java; but did not. The reason is by performance, executing length variable is speedier than calling the method length(). It is like comparing two strings with == and equals(). equals() is a method call which takes more time than executing == operator.
size() method:
It is used to find the number of elements present in collection classes. It is defined in java.util.Collection interface.

- 1,342
- 3
- 15
- 33
Based on the syntax I'm assuming that it is some language which is descendant of C. As per what I have seen, length
is used for simple collection items like arrays and in most cases it is a property.
size()
is a function and is used for dynamic collection objects. However for all the purposes of using, you wont find any differences in outcome using either of them. In most implementations, size simply returns length property.

- 4,339
- 2
- 27
- 51
-
-
That tag has been added after my answer, so I couldn't have known. All the other answers which target java are given after a year of the question. – Buddha Sep 11 '17 at 08:42
-
The source code in the question has always been Java ... even if you didn't recognize it as such. So your your answer has always been inapplicable. Either way, it now serves no purpose and should be deleted. – Stephen C Sep 11 '17 at 09:39
-
You are also incorrect about the tagging. According to the edit history, it was added in "Feb 14 '15 at 3:00". Two (possibly three) answers other than yours (including a deleted one) that **predate** the `java` tag have correctly identified this as Java. – Stephen C Sep 11 '17 at 09:45
-
@StephenC This is getting ridiculous. Can you see that I have answered this question on Nov 25th, **2013**, 4 minutes after the question is added, When no such tag information is available? You yourself told that tag is added in 2015. Some users do assume programming language and give answer. I just gave what I think is a generic enough explanation so that it helps the candidate to the best of my knowledge. I see you have helped the community a lot. So, I respect you for that. But I would rather help other people by answering than arguing with you to boost my ego about a non-issue. – Buddha Sep 13 '17 at 01:36
-
I have down-voted it because it is now objectively unhelpful. If your aim is to help people, then the appropriate course would be for you to delete it. – Stephen C Sep 13 '17 at 02:36
-
Fine by me. In my view, it is not a bad answer so not going to delete it. – Buddha Sep 13 '17 at 04:20
I bet (no language specified) size()
method returns length
property.
However valid for
loop should looks like:
for (int i = 0; i < values.length; i++) {}

- 148,279
- 62
- 259
- 315