Where is the length property of the array defined?

Asked 2 years ago, Updated 2 years ago, 53 views

ArrayList<Integer> arr = new ArrayList(10);
 int size = arr.size(); 

In order to obtain the length of the ArrayList in this way, the size() method is used.

And

String[] str = new String[10];
int size =  str.length;

This is how you get the length of the Array object.

ArrayList's size() class is defined in the ArrayList class So where is the length property of the array defined? Is it implemented by JVM or defined in the java API class?

java array

2022-09-22 22:23

1 Answers

Array is a Java object. length is a simple final variable. If you look at the class file in which the class of the array is defined,

length is a public final field that contains the length of the array. The value can be zero or positive.

It's defined in this way.

http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7 See here for details.


2022-09-22 22:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.