-3

Where is the difference between those two kinds of declaration for String s:

1)

public static void main(String[] args) {
    String s;

    for (int i = 0; i < 1000000; i++) {
        s = "String" + i;
        System.out.println(s);
    }
}

2)

public static void main(String[] args) {
    for (int i = 0; i < 1000000; i++) {
        String s = "String" + i;
        System.out.println(s);
    }
}

My task-manager says the first one doesn't need as much as CPU as the second one. So Java compiles the two samples in two different ways? But how.

Thank you for your help!

4
  • 8
    Task manager isn't exactly the best metric for measuring performance Commented Dec 17, 2015 at 17:59
  • 1
    The task manager is next to useless. The two are the same. The latter is preferred for clarity of purpose. Commented Dec 17, 2015 at 18:01
  • Take a look at this link Commented Dec 17, 2015 at 18:02
  • I dont think there is much difference apart from scope of variable but for me both approches are bad you can use StringBuilder instead for concatenation that much within the loop Commented Dec 17, 2015 at 18:02

1 Answer 1

8

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
Sign up to request clarification or add additional context in comments.

1 Comment

My answer did seem familiar, so marked question as duplicate of stackoverflow.com/questions/33357585/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.