wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes How many String objects created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How many String objects created" Watch "How many String objects created" New topic
Author

How many String objects created

Gaurav Kshatriya
Ranch Hand

Joined: Mar 07, 2010
Posts: 33
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.


Thanks
Gaurav.... Cheers....
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3418
    
  12
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.


Joanne
Gaurav Kshatriya
Ranch Hand

Joined: Mar 07, 2010
Posts: 33
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

Joined: Aug 24, 2010
Posts: 35
3 objects
Gaurav Kshatriya
Ranch Hand

Joined: Mar 07, 2010
Posts: 33
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

Joined: Sep 15, 2010
Posts: 1
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

Joined: Mar 07, 2010
Posts: 33
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

Joined: Oct 04, 2010
Posts: 19
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!.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many String objects created
 
Similar Threads
GC Doubt
Garbage collection doubt
== doubt
srting doubt on ==
HFJ doubt