Musings…


for(;;) and while(true). Is there any difference?
November 10, 2005, 8:01 am
Filed under: Musings on Tech stuff, Java, Linux and Open Source

Gideon, a member of our 4 yr old PinoyJUG mailing list used javap to see if there was a difference in the bytecodes.

Quoting his test:

It seems that javac generates the same byte code for both these
implementation of endless loops.

–begin While.java–
public class While {

public static void main() {
while (true) {
}
}

}
–end While.java–

output of “javap -c While.java”
–begin–
public class While extends java.lang.Object{
public While();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.”":()V
4: return

public static void main();
Code:
0: goto 0

}
–end–

–begin ForLoop.java–
public class ForLoop {

public static void main() {
for(;;);
}

}
–end ForLoop.java–

output of “javap -c ForLoop.java”
–begin–
public class ForLoop extends java.lang.Object{
public ForLoop();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object.”":()V
4: return

public static void main();
Code:
0: goto 0

}
–end–

According to his test there isn’t any difference in the bytecode generated. Well there’s this guy who is debating that there is a difference in terms of faster processing of the controversial contructs. Is there really a difference? Maybe someone from Sun can enlighten us regarding this matter.

So whatchathink? Which is faster?


2 Comments so far
Leave a comment

everything is slow when you’re swing-ing anyway hhehehe…

Comment by Dengoy

for (;;) and while(true) means the same thing. I would start doubting Sun’s competence if they didn’t result in the same opcodes.

Its like when you (yes you) see 2 beautiful girls walking in the street, in your mind the image results in the same thought!!!

Comment by Nevyn




Leave a comment
Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>