10

For the below statement in a program, how many objects will be created in heap memory and in the string constant pool?

I need clarity in object creation. Many sources I've read are not elaborating. I am confused when the object gets destroyed.

String a="MAM"+"BCD"+"EFG"+"GFE";

How many objects will be created?

I am looking for good material about the life cycle of objects, methods and classes and how the JVM handles them when they are dynamically changed and modified.

Matsemann
  • 21,083
  • 19
  • 56
  • 89
saikiran
  • 2,247
  • 5
  • 27
  • 42
  • Someone should just run `javah` on it. I'd bet it would show only one constant. – Rekin Aug 07 '13 at 07:17
  • upto me only one but many friends in this discussion confused me [link](https://www.facebook.com/groups/java.for.developers/10151748401034501/?notif_t=group_comment_reply) – saikiran Aug 07 '13 at 07:18

6 Answers6

20

"MAM"+"BCD"+"EFG"+"GFE" is a compile-time constant expression and it compiles into "MAMBCDEFGGFE" string literal. JVM will create an instance of String from this literal when loading the class containing the above code and will put this String into the string pool. Thus String a = "MAM"+"BCD"+"EFG"+"GFE"; does not create any object, see JLS 15.18.1. String Concatenation Operator +

The String object is newly created (§12.5) unless the expression is a compile-time constant expression (§15.28).

It simply assigns a reference to String object in pool to local var a.

giampaolo
  • 6,906
  • 5
  • 45
  • 73
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
6

Only one object is created.

string s1 = "java";
string s2 = "ja" + "va";
s.o.p(s1==s2);

The statement yields true.

String s1="java";
string s2 = "ja";
String s3 = s2 +"va";
s.o.p(s1==s3);

The statement yields false.

So minimum one apparent should be permanent, then '+' operator generates new string object (in non constant pool using new()). So, the question you asked does not have one also permanent. This means it creates only one object.

Corey
  • 398
  • 1
  • 4
  • 18
Siva Kumar Reddy G
  • 1,274
  • 3
  • 18
  • 32
3

Exactly one object is created and placed in the constant pool, unless it already exists, in which case the existing object is used. The compiler concatenates string constants together, as specified in JLS 3.10.5 and 15.28.

A long string literal can always be broken up into shorter pieces and written as a (possibly parenthesized) expression using the string concatenation operator +

http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.5

Joni
  • 108,737
  • 14
  • 143
  • 193
3

Most answers seem to focus that a) the complete expression is one compile time constant and b) that the line itself does not construct a new object but only a reference to one object.

However noone so far has mentioned, that String itself contains a reference to an internal char[] (which is also in the constant pool).

Summary: There are two objects in the constant pool (String and char[]). The line does neither create nor destroy any object.

And regarding:

I am confused when the object gets destroyed.

No object is destroyed, since stuff in the constant pool will only be destroyed if the class itself would be unloaded. At most you can say, that the reference a will go out of scope eventually.

A.H.
  • 63,967
  • 15
  • 92
  • 126
2

Only one object will be created since String a will compile into "MAMBCDEFGGFE".

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0

Answers stating a single heap object in your example are correct. However, consider this code:

public class Tester
{
   public String a="MAM";
   public String b ="BCD";
   public String c = "EFG";
   public String d ="GFE";

   public Tester()
   {
      String abcd = a + b + c + d;
   }
}

In this example, there are 7 strings being created. a,b,c and d are not compiled into a single constant - they are members. 1 string is then created for each + operator - semantically speaking, + is a concatenation but logically it is creating a new string in memory. The first 2 operator strings are discarded immediately and are now eligible for garbage collection but the memory churn still occurs.

Technically there in an 8th object. The instance of Tester.

Edit: This has been proved to be nonsense in the comments

Gusdor
  • 14,001
  • 2
  • 52
  • 64
  • 3
    The javac compiler has an optimization: Not each `+` generates a new string. Instread one internal `StringBuilder` is created, `a`, `b`, `c`, `c` are appended and then one `String` is created. This optimization has been around for more than a decade. So the calculation is 5 `String` and 5 associated `char[]`, 1 `StringBuilder` plus at least one associated `char[]` - eventually more due to dynamic resizing. – A.H. Aug 07 '13 at 09:57
  • u mean it will first append a+b and then consider a+b as one string and append to c ?? i didn't get you – saikiran Aug 07 '13 at 13:41