This article is participating in “Java Theme Month – Java Debug Notes Event”, see < Event link > for more details.
Question: Where is the length property of an array defined?
We can use its public method size() to determine the length of ArrayList <E>, for example
ArrayList<Integer> arr = new ArrayList(10);
int size = arr.size();
Copy the code
Similarly, we can use the Length attribute to determine the length of an Array object
String[] str = new String[10];
int size = str.length;
Copy the code
The size () method of ArrayList is defined inside the ArrayList class, but where is the length property of Array defined?
Answer 1:
Arrays are special objects in Java that have a simple property called Length, which is final.
There is no “class definition” for arrays (you can’t find it in any.class file); they are part of the language itself. (Can be interpreted as something like a basic data type)
The members of array types are all of the following:
The members of an array type are all of the following: public final field Length, which contains the number of components in the array. The length can be positive or zero. The public method clone(), which overrides classesObjectMethod with the same name in, and does not throw any checked exceptions. The clone method of array type T [] returns type T []. A clone of a multidimensional array is a shallow clone, that is, it creates only a new array. Subarrays are shared. All members are fromObjectClass inheritance; The only way to uninherit an object is to clone it.Copy the code
Additional notes in the comments section:
Why aren’t arrays like objects? Because of history. Most attempts at language innovation fail when Java is designed with syntax and style dissimilar to C. As a result, C ++, Objective-C, and Java were one of the few languages to avoid obscurity in their day. Java’s design was consciously designed to include curly braces, primitive data types, and pure arrays to be familiar to today’s mainstream programmers.