代码1:
int num;
for (num =0; num<10; num++){
System.out.println(num);
}
代码2:
for (int num=0; num<10; num++){
System.out.println(num);
}
答案 0 :(得分:3)
它们编译相同,因此具有相同的性能和内存要求。
为了证明使用javap -c <classfile>
代码:
public static void a() {
for (int num = 0; num < 10; num++) {
System.out.println(num);
}
}
public static void b() {
int num;
for (num = 0; num < 10; num++) {
System.out.println(num);
}
}
字节代码:
public static void a();
Code:
0: iconst_0
1: istore_0
2: goto 15
5: getstatic #15 // Field java/lang/System.out:Ljava/io/PrintStream;
8: iload_0
9: invokevirtual #21 // Method java/io/PrintStream.println:(I)V
12: iinc 0, 1
15: iload_0
16: bipush 10
18: if_icmplt 5
21: return
public static void b();
Code:
0: iconst_0
1: istore_0
2: goto 15
5: getstatic #15 // Field java/lang/System.out:Ljava/io/PrintStream;
8: iload_0
9: invokevirtual #21 // Method java/io/PrintStream.println:(I)V
12: iinc 0, 1
15: iload_0
16: bipush 10
18: if_icmplt 5
21: return