There is no difference!
The generated bytecode is the same, except that declaring the fields in opposite order makes them get assigned different "register" numbers.
Naming your two methods test1
and test2
, then compiling and decompiling the code (using javap -c
), shows this:
public static void test1();
Code:
0: iconst_0
1: istore_1
2: goto 32
5: new #22 // class java/lang/StringBuilder
8: dup
9: ldc #24 // String String
11: invokespecial #26 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
14: iload_1
15: invokevirtual #29 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
18: invokevirtual #33 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
21: astore_0
22: getstatic #37 // Field java/lang/System.out:Ljava/io/PrintStream;
25: aload_0
26: invokevirtual #43 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
29: iinc 1, 1
32: iload_1
33: ldc #48 // int 1000000
35: if_icmplt 5
38: return
public static void test2();
Code:
0: iconst_0
1: istore_0
2: goto 32
5: new #22 // class java/lang/StringBuilder
8: dup
9: ldc #24 // String String
11: invokespecial #26 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
14: iload_0
15: invokevirtual #29 // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
18: invokevirtual #33 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
21: astore_1
22: getstatic #37 // Field java/lang/System.out:Ljava/io/PrintStream;
25: aload_1
26: invokevirtual #43 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
29: iinc 0, 1
32: iload_0
33: ldc #48 // int 1000000
35: if_icmplt 5
38: return