Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many String objects created

 
Gaurav Kshatriya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am having a doubt in my mind regarding String objects. Its like :

String s = "ABC" + "DEF" + "GNH" + "HNG";

I want to know how many String objects are created in this case. 4 are there no doubtedly. My doubt is whether all the concatenation takes place in a single go (i.e 5 objects) or its like first two will get concatenated and an object will create and so on ( i.e 7 objects totally)

Please help.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Kshatriya wrote:Hi

I am having a doubt in my mind regarding String objects. Its like :

String s = "ABC" + "DEF" + "GNH" + "HNG";

I want to know how many String objects are created in this case. 4 are there no doubtedly. My doubt is whether all the concatenation takes place in a single go (i.e 5 objects) or its like first two will get concatenated and an object will create and so on ( i.e 7 objects totally)

Please help.


Actually only 1 object will be created.
The compiler will optimise that line to
String s = "ABCDEFGNHHNG";

And you could argue that that line of code doesn't produce any objects, as the String would actually have been created when the class was loaded and so would already exist when that line was executed.
 
Gaurav Kshatriya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for clearing the doubt Joanne.

One more help if the code is like this :

String s1 = "Hi ";
String s2 = "Hello " + "How ";
String s3 = s2 + "are";
System.out.println(s3 + " " + "you");

Now how many string objects will be there. It would clear all my doubts regarding String creation.
 
Harnoor Singh
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
3 objects
 
Gaurav Kshatriya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Harnoor.

Can you please elaborate the answer ?? I am not able to identify how and when these 3 objects will be created ??

 
Venkatesh Tl
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Kshatriya wrote:Thanks Harnoor.

Can you please elaborate the answer ?? I am not able to identify how and when these 3 objects will be created ??

Since you have created 3 objects of string, 3 objects are created and the "are" string is treated as immediate value, i suppose
 
Gaurav Kshatriya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Venkatesh,

There are 3 references of String. Actual no. of objects are greater than 3 and they are lost in memory as there is no reference to them. So i want to know all the object whether they are referenced or not.
 
Baichuan Huang
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gaurav Kshatriya wrote:Hi

I am having a doubt in my mind regarding String objects. Its like :

String s = "ABC" + "DEF" + "GNH" + "HNG";

I want to know how many String objects are created in this case. 4 are there no doubtedly. My doubt is whether all the concatenation takes place in a single go (i.e 5 objects) or its like first two will get concatenated and an object will create and so on ( i.e 7 objects totally)

Please help.

i think just one,
when the jvm is compileing,it will make "ABC" + "DEF" + "GNH" + "HNG" change to "ABCDEFGNHHNG"
so ,it will create a Object valued "ABCDEFGNHHNG" in Object pool.
hope this helps!.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic