I was looking at a project in java and found a for
loop which was written like below:
for(int i=1; i<a.length; i++)
{
...........
...........
...........
}
My question is: is it costly to calculate the a.length
(here a is array name)? if no then how a.length
is getting calculated internally (means how JVM make sure O(1) access to this)? Is is similar to:
int length = a.length;
for(int i=1; i<length; i++)
{
...........
...........
...........
}
i.e. like accessing a local variable's value inside the function. Thanks.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…