English 中文(简体)
汇编者产生的 lo如何使用代典
原标题:How is Java s for loop code generated by the compiler
  • 时间:2010-08-08 06:08:46
  •  标签:
  • java
  • foreach

Java如何用于汇编者产生的代典法?

例如,如果我有:

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

<代码>getStringArray() 是恢复我所希望的Array(Array I)的功能,是永远还是仅仅一次运转? 使用这一建筑的住所法如何最佳?

问题回答

On the semantics of enhanced for loop

此处为Java 语言规格3rd Edition的相关摘录,稍作编辑,以澄清:

JLS 14.14.2 The enhanced for statement

for ( Type Identifier : Expression ) Statement

如果Expression的类型为阵列,T[],则<>for的强化代码的含义由<>for>>以下基本代码给出:

T[] a = Expression;
for (int i = 0; i < a.length; i++) {
    Type Identifier = a[i];
    Statement
}

<代码>a和i>均为汇编者生成的识别资料,与在<<>for上添加的标识(编造或以其他方式生成)不同。

So in fact the Language does guarantee thatcode>Expression <>m>只评价一次。

全文见<代码>Expression为的等值。 可检索:

JLS 14.14.2 The enhanced for statement

for ( Type Identifier : Expression ) Statement

如果<代码>Expression的类型为Iterable的亚类,则请将I改为Expression.iterator(>

for (I iter = Expression.iterator(); iter.hasNext(); ) {
    Type Identifier = iter.next();
    Statement
}

<代码>iter为汇编者生成的识别符号,不同于在<<<>for>上添加的标识(编造或以其他方式生成的识别符号)。

请注意,如果<代码>Expression <>/code>既不是Iterable,也不是一个阵列,那么上述两种情形中你只能使用<>for的强化代码。 此外,为了明确起见,上述引文排除了<<>条码/代码>上的任何标签以及<条码>证书/代码上的任何缩略语者的信息,但这些标签按预期处理。


On the performance of enhanced for loop

页: 1 Effective Java 2nd Edition, 项目46: Prefer for-each loops to traditional for loops

第1.5号版的“前ach”通过藏匿器或完全指数变数,消除了utter和错误机会。 由此而产生的异构体同样适用于收集和阵列。 Note that is no exercise penalty for using the for-each loop,哪怕是阵列。 事实上,在某些情况下,它可能提供与普通<<>条码/代码>相比的绩效优势,因为它只计算了阵列指数的限额。 虽然您是“<<>can>/em>,但方案人员并不总是这样做。

因此,这本书声称,事实上有些汇编者超越了联合专业学校的翻译工作,并且对每个编程进行了更多的优化(当然,尽管仍然保留其语义)。

简言之,你不应对ach的表现感到担忧。 语言的具体说明是明智的(Expression, 仅经过一次评价), 原文是“许多假想中的首选结构”,汇编者将确保尽可能优化这些内容。

See also

JDK 1.4介绍了<代码>RandomAccess接口。 目的是在某一特定的<代码>上对算法进行跟踪。 清单执行,通过下列方式提高操作效率:

for (int i = 0, n = list.size(); i < n; i++) {
    list.get(i);
}

页: 1

for (Iterator i = list.iterator(); i.hasNext(); ) {
   i.next();
}

for是否考虑到这一点? 还是完全无视这样的事实,即某一可核查者事实上是名单?

应当指出,这将意味着增加<条码>(可论证的事例)。 List && Iterable instance of RandomAccess) test and adowncast to 清单将发挥作用,增加不总是值得的间接费用,并可能被视为编辑的早优化意料甘>。

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——答复与收到的评论意见有改动。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...