java - Does shorthand for loop cache the iterable's reference? -


i trying over-efficient, have been wondering of following 2 code samples execute more quickly.

assume have reference object contains arraylist of strings , want iterate through list. of following more efficient (even if marginally so)?

for(string s : foo.getstringlist())     system.out.println(s); 

or

arraylist<string> stringarray = foo.getstringlist(); for(string s : stringarray)     system.out.println(s); 

as can see, second loop initializes reference list instead of calling every iteration, seems first sample does. unless notion incorrect , both function same way because inner workings of java create own reference variable.

so question is, it?

there's no difference, because both loops end getting , maintaining reference iterator expression @ start of loop. here's excerpt java language specification:

the enhanced statement equivalent basic statement of form:

for (i #i = expression.iterator(); #i.hasnext(); ) {     variablemodifiersopt targettype identifier =         (targettype) #i.next();     statement } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -