Java – length in array and length () in string

When we talk about arrays and length (), why is the length of data fields when we talk about strings in Java? Means:

int a[10] = {1,2,3,4,5,6,7,8,9,10};
String str = "foo";
int a_len = a.length;
int str_len = str.length();

Why is length not a function in the case of arrays and vice versa?

Solution

In short, this is just its way, and it has always been so

The array specified in JLS section 10.7 has a common final length field It may be specified as a method because of consistency - but it's just not... Similarly, string may have made an implementation decision with a common final length field - but it doesn't happen again

There have been some inconsistencies since 1.0 - obviously these things can't be changed after release

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>