How is Java's for loop code generated by the compiler

前端 未结 4 1619
再見小時候
再見小時候 2020-12-06 04:57

How is Java\'s for loop code generated by the compiler?

For example, if I have:

for(String s : getStringArray() )
{
   //do something with s
}
         


        
4条回答
  •  甜味超标
    2020-12-06 05:16

    Compiler might call it just once, but you can depend on it. It may not be a good coding practice. If getStringArray() returns same array each time, why not set to a variable first?

    EDIT - answer changed with the comments received.

提交回复
热议问题