Java for 循环中的两个分号是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5676992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What do two semicolons mean in Java for loop?
提问by Shawn
I was looking inside the AtomicInteger
Class and I came across the following method:
我正在查看AtomicInteger
Class内部,发现了以下方法:
/**
* Atomically increments by one the current value.
*
* @return the previous value
*/
public final int getAndIncrement() {
for (;;) {
int current = get();
int next = current + 1;
if (compareAndSet(current, next))
return current;
}
}
Can someone explain what for(;;)
means?
有人能解释一下是什么for(;;)
意思吗?
回答by Bozho
It is equivalent to while(true)
.
它相当于while(true)
。
A for-loop has three elements:
for 循环包含三个元素:
- initializer
- condition (or termination expression)
- increment expression
- 初始化程序
- 条件(或终止表达式)
- 增量表达式
for(;;)
is not setting any of them, making it an endless loop.
for(;;)
没有设置它们中的任何一个,使其成为无限循环。
Reference: The for statement
参考:for 语句
回答by Alberto Zaccagni
It's the same thing as
这与
while(true) {
//do something
}
...just a little bit less clear.
Notice that the loop will exit if compareAndSet(current, next)
will evaluate as true
.
……只是有点不太清楚。
请注意,如果compareAndSet(current, next)
将评估为,则循环将退出true
。
回答by Esko
It's just another variation of an infinite loop, just as while(true){}
is.
它只是无限循环的另一种变体,就像现在一样while(true){}
。
回答by John Kane
That is a for ever loop. it is just a loop with no defined conditions to break out.
这是一个永远循环。它只是一个没有定义条件的循环。
回答by Isaac Truett
It's an infinite loop, like while(true)
.
这是一个无限循环,就像while(true)
.