Why do we have the length of an array as an attribute, array.length
, and for String we have a method, str.length()
?
Is there some reason?
length() in Java is a final method, which is applicable for string objects. You can use it to find the number of characters in a string. For example, string. length() will return the number of characters in “string.”
So yeah, there is no difference, despite the time the subtraction of 1 will take, and this can be ignored.
Difference between length of array and size() of ArrayList in Java. ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array.
What is the difference between the size of ArrayList and length of Array in Java? ArrayList doesn't have length() method, the size() method of ArrayList provides the number of objects available in the collection. Array has length property which provides the length or capacity of the Array.
Let me first highlight three different ways for similar purpose.
length
-- arrays (int[]
, double[]
, String[]
) -- to know the length of the arrays
length()
-- String related Object (String
, StringBuilder
, etc) -- to know the length of the String
size()
-- Collection Object (ArrayList
, Set
, etc) -- to know the size of the Collection
Now forget about length()
consider just length
and size()
.
length
is not a method, so it completely makes sense that it will not work on objects. It only works on arrays.size()
its name describes it better and as it is a method, it will be used in the case of those objects who work with collection (collection frameworks) as I said up there.
Now come to length()
:
String is not a primitive array (so we can't use .length
) and also not a Collection (so we cant use .size()
) that's why we also need a different one which is length()
(keep the differences and serve the purpose).
As answer to Why?
I find it useful, easy to remember and use and friendly.
A bit simplified you can think of it as arrays being a special case and not ordinary classes (a bit like primitives, but not). String and all the collections are classes, hence the methods to get size, length or similar things.
I guess the reason at the time of the design was performance. If they created it today they had probably come up with something like array-backed collection classes instead.
If anyone is interested, here is a small snippet of code to illustrate the difference between the two in generated code, first the source:
public class LengthTest {
public static void main(String[] args) {
int[] array = {12,1,4};
String string = "Hoo";
System.out.println(array.length);
System.out.println(string.length());
}
}
Cutting a way the not so important part of the byte code, running javap -c
on the class results in the following for the two last lines:
20: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
23: aload_1
24: arraylength
25: invokevirtual #4; //Method java/io/PrintStream.println:(I)V
28: getstatic #3; //Field java/lang/System.out:Ljava/io/PrintStream;
31: aload_2
32: invokevirtual #5; //Method java/lang/String.length:()I
35: invokevirtual #4; //Method java/io/PrintStream.println:(I)V
In the first case (20-25) the code just asks the JVM for the size of the array (in JNI this would have been a call to GetArrayLength()) whereas in the String case (28-35) it needs to do a method call to get the length.
In the mid 1990s, without good JITs and stuff, it would have killed performance totally to only have the java.util.Vector (or something similar) and not a language construct which didn't really behave like a class but was fast. They could of course have masked the property as a method call and handled it in the compiler but I think it would have been even more confusing to have a method on something that isn't a real class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With