Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (4k points)

We can determine the length of an ArrayList<E> using its public method size(), like

ArrayList<Integer> arr = new ArrayList(10);

int size = arr.size();

Similarly we can determine the length of an Array object using the length property

String[] str = new String[10];

int size =  str.length;

Whereas the size() method of ArrayList is defined inside the ArrayList class, where is this length property of Arraydefined?

1 Answer

0 votes
by (46k points)

Arrays are unique objects in java, they have a mere attribute named length which is final.

There is no "class definition" of an array (you can't locate it in any .class file), they're a piece of the writing itself.

10.7. Array Members

The members of an array type are all of the following:

  • The public final field length, which contains the number of components of the array. length may be positive or zero.

  • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].

  • A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

  • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

Resources:

Related questions

0 votes
1 answer
asked Sep 19, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Sep 19, 2019 in Java by Anvi (10.2k points)
+1 vote
2 answers
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...