They're two completely different things.
.length is a property on arrays. That isn't a method call.
.length() is a method call on String.
You're seeing both because first, you're iterating over the length of the array. The contents of the array are String, and you want to add up all of their lengths, so you then call the length on each individual String in the array.
Videos
They're two completely different things.
.length is a property on arrays. That isn't a method call.
.length() is a method call on String.
You're seeing both because first, you're iterating over the length of the array. The contents of the array are String, and you want to add up all of their lengths, so you then call the length on each individual String in the array.
.length is an array property. .length() is a method for the class String (look here).
When you are looping, you are looping for the length of the array using people.length.
But when you are using people[i].length(), you are accessing the string at that position of the array, and getting the length of the string, therefore using the .length() method in the String class.
However, just to confuse you more, a String at its core is just an array of chars (like this: char[]). One could make the argument that .length should work as well, considering it is an array of characters, however, it is a class and that is the reason .length will not work. Showing empty parameters shows it's a method, and showing no parameters shows that it's a property (like a static variable in a class).
In Java, there are functions that end with parentheses and there are some that don't, like x.length which is only used for arrays and x.length() only for strings. In Ruby, this is exactly the opposite. In JavaScript, x.length is used for both arrays and strings, so without parentheses, but then it's x.toUpperCase() which ends with parentheses...
And then in Python, it's len(x) (it could very well have been length(x)), where the variable is put inside the parenthesis as parameter to get the length of a string/array, but then it's x.upper() which again puts the variable outside of parentheses...
All languages aforementioned are objected oriented, but have their own way of calling upon the length functionality, which is super confusing. Is there a logic to why it's x.function, x.function(), or function(x)?
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.
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.
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.
Hi, I've been trying to find a name for my method that returns the length of an object. A quick google search showed that it's conventional to name getters getX(). However, when I investigated the first standard class that came to mind - String, I found it having the length() method instead of expected getLength(). Why is that? How should I name a length method in my own class then?
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#length--