1
String str = "test"; str = str + "test2"; str = str + "test3"; str = str + "test4"; str = str + "test5"; 

How many objects will be created from the above code and how many objects will be available for garbage collection?

Can someone please explain this?

2

2 Answers 2

0

How many objects will be created

At runtime, 4, i.e. the four computed values of str, excluding the initial value which comes from the constant pool.

and how many objects will be available for garbage collection?

At the end of this code but before str goes out of scope, three, i.e. the three intermediate values of str.

Note that I am counting Strings. Each String will have an associated char[] which is another object.

However if the surrounding code is such that the JVM can determine that str cannot change between these lines of code it could be as low as one and zero respectively.

Sign up to request clarification or add additional context in comments.

2 Comments

I think String str = "test"; will also create an object in String Constant pool, that is located inside Heap memory of JVM. So the no. of objects will be 5.
@RohitGaikwad But not at runtime when this code is executed. It happens at class-loading time.
0

The JavaC is very odd regarding String manipulation. For example, why dont they use String.concat when you do "String += otherString"?

Instead, Java creates a StringBuilder (or StringBuffer, depending on the Java version) for each line ended with ; that you have concatenating Strings.

I placed your code in a test program (TCTestWin) and called from command line:

javap -c TCTestWin.class 

These are the results:

 0: ldc #15 // String test 2: astore_1 3: new #17 // class java/lang/StringBuilder 6: dup 7: aload_1 8: invokestatic #19 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String; 11: invokespecial #25 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V 14: ldc #28 // String test2 16: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 19: invokevirtual #34 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 22: astore_1 23: new #17 // class java/lang/StringBuilder 26: dup 27: aload_1 28: invokestatic #19 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String; 31: invokespecial #25 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V 34: ldc #38 // String test3 36: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 39: invokevirtual #34 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 42: astore_1 43: new #17 // class java/lang/StringBuilder 46: dup 47: aload_1 48: invokestatic #19 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String; 51: invokespecial #25 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V 54: ldc #40 // String test4 56: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 59: invokevirtual #34 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 62: astore_1 63: new #17 // class java/lang/StringBuilder 66: dup 67: aload_1 68: invokestatic #19 // Method java/lang/String.valueOf:(Ljava/lang/Object;)Ljava/lang/String; 71: invokespecial #25 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V 74: ldc #42 // String test5 76: invokevirtual #30 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 79: invokevirtual #34 // Method java/lang/StringBuilder.toString:()Ljava/lang/String; 82: astore_1 83: return 

As you can see, for each line, JavaC creates a StringBuilder, appends the String, and continues.

Such code, specially when used inside a loop, will rush the Garbage Collector.

There are three better ways to do it:

  1. Use concat instead:

    String str = "test"; str = str.concat("test2"); str = str.concat("test3"); str = str.concat("test4"); str = str.concat("test5"); 
  2. Use a single line of concatenation, which will create a single StringBuilder. Remember that each ; will create another StringBuilder. Note that in the concatenation of constant Strings below, the Java compiler will create a single String. However, if you add one or more String variables, then the StringBuilder will be created.

    String str = "test" + "test2" + "test3" + "test4" + "test5"; 
  3. Create a StringBuilder yourself, do the concatenation, and REUSE the StringBuilder. This has the best performance, specially when doing things in a loop.

    StringBuilder sb = new StringBuilder(512); for (int i = 0; i < 10000; i++) { sb.setLength(0); sb.append("test"); sb.append("test2"); sb.append("test3"); sb.append("test4"); sb.append("test5"); sb.append(i); String s = sb.toString(); } 

So, from the code above, 4 different StringBuilders will be created. After the final String, all StringBuilders will be collected.

5 Comments

If you use numbering or bullet points, following code needs to be double-indented.
They don't use multiple String.concat() calls because a single StringBuilder is considerably more efficient. There is nothing 'very odd' about a compiler choosing better object code than programmers can think of. That's what they're for. NB You haven't actually answered the question.
Completely agreed if... there a single StringBuilder was created. However, the code that was posted does NOT create a single StringBuilder, but 4!
If String str = "test" + "test2" + "test3" + "test4" + "test5" creates a single StringBuilder, I'm throwing out the compiler and getting a different one. This should not create any StringBuilder. Java has rules that say that the right-hand side is a constant expression (which makes a difference in some cases, since there are places where only constant expressions are allowed). Because of that, the compiler should already know that it has to process this as a constant, and as a result, it should create one string, "testtest2test3test4test5".
@ajb, you're right. Adding String constants will make java create a single String at compile time. However, in real world, i believe he will not be adding constants, but different variables. The codes i sent will teach him what can happen in real code when adding , not this one that he sent.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.