In Java, why is the method length for strings, while it is a variable for arrays?

Possible duplicate:
Why is the String.length () method?

If I want to know the length of the array, I will need to enter the following:

array.length 

Therefore, this is a variable in which the length of the array with the name "array" is written.

In Strings, the same would look like this:

 string.length() 

Therefore, this is a method that returns the length of the variable 'string'.

But why did they decide to make these two different, while basically he does the same ... Is it because of efficiency or why else?

Thanks in advance.

+4
source share
1 answer

Good question.

Arrays

are fixed in size and therefore have a public field of finite length.

Lines

implement the CharSequence interface, which has a length () function that must be implemented.

+1
source

All Articles