Re: How to instantaneously convert array of Integers into an array
on int's?
Lew allegedly wrote:
So how much time does declaring a length variable for the array length
variable save in reality?
Daniele Futtorovic wrote:
Typing time? None. Execution time? Probably none, either.
That's for Array#length. String#length() and Collection#size() are
another matter.
Whether Hotspot can optimize the methods, like the ability to declare a
separate variable oneself for those methods, depends on whether the loop
alters the String reference or Collection reference or contents.
Since Strings are immutable, if the reference doesn't change in the loop then
Hotspot would be able to treat the 'length()' value as a constant.
If one loops through a Collection using an iterator and the 'size()' changes,
that would throw a 'ConcurrentModificationException' if the iterator didn't
make the change. That and legal size changes would prevent the compiler and
the coder from using a separate variable for the size, if those things could
be present in the loop body. If they cannot be present in the loop body then
a coder could use a size variable, and Hotspot would have enough information
to do so, too. Hotspot would be in a better position than a coder, because
Hotspot can tell at run time if any method calls in the loop body might change
the size, even if that cannot be determined at compile time.
--
Lew