Didn't I put it in the title?! Well, this is my blow-by-blow account of my experience with Java, Linux and other Copyleft stuff. Blogging to take the FUD away!

Thursday, November 10, 2005

for(;;) and while(true). Is there any difference?

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:

Blogger Dengoy said...

everything is slow when you're swing-ing anyway hhehehe...

12:18 AM

 
Blogger Alwyn Schoeman said...

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!!!

2:01 AM

 

Post a Comment

<< Home