其他
为什么JDK源码中,无限循环大多使用for(;;)而不是while(true)?
mymbp:/Users/me/workspace/jdk8u/jdk/src
$ egrep -nr "for \(\s?;\s?;" . | wc -l
369
mymbp:/Users/me/workspace/jdk8u/jdk/src
$ egrep -nr "while \(true" . | wc -l
323
while (1) {
/* ... */
}
至于Java里的for (;;)与while (true),哪个更快?
public void foo() {
int i = 0;
while (true) { i++; }
}
/*
public void foo();
Code:
stack=1, locals=2, args_size=1
0: iconst_0
1: istore_1
2: iinc 1, 1
5: goto 2
*/
public void bar() {
int i = 0;
for (;;) { i++; }
}
/*
public void bar();
Code:
stack=1, locals=2, args_size=1
0: iconst_0
1: istore_1
2: iinc 1, 1
5: goto 2
*/