• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How many String objects created

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 objects
 
Gaurav Kshatriya
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!.
 
Hot dog! An advertiser loves us THIS much:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic