aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how many String objects are created Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how many String objects are created" Watch "how many String objects are created" New topic
Author

how many String objects are created

Loveleen Saroya
Ranch Hand

Joined: Aug 08, 2005
Posts: 42
How many string objects are created in the following code:-



1)2
2)4
3)8
4)9
im confuse that in System.out.println statement 2 objects r created or 1.and in this line s1.concat("fall"); one or two String objects are created.
one more doubt.. if we put the code in the main method of a class will the object created be increase by 1 as the main method takes array of string as parameter.so the empty string.




thanks in advance
SCJP 1.4 in progress
Loveleen
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hello Loveleen,

I think here 8 objects are created in this code...although i m not sure of it.

String s1 = "spring";
String s2 = s1+"summer";
s1.concat("fall");
s1.concat(s2);
s1+="winter";
System.out.println(s1+" "+s2);

1) "spring" in pool referenced by s1
2) "summer" in pool
3) "springsummer" (s1+"summer") referenced by s2
4) "fall" in pool
5) "springfall" (s1.concat("fall")) object created although not stored in any variable
6) "springspringsummer" (s1.concat(s2))object created although not stored in any variable
7) "winter" in pool
8) "springwinter" (s1+="winter")referenced by s1
9) "springwinter " and
10) "springwinter springsummer" two objects created in println statement
s1+" " and whatever the result is + s2

according to me 8 would be the correct answer.
Correct me guys if i am wrong..coz i am not 100% sure of it.

Loveleen you might have got some idea.

Sandy

[ September 13, 2005: Message edited by: Sandeep Chhabra ]
[ September 13, 2005: Message edited by: Sandeep Chhabra ]

Regards<br />Sandy<br />[SCJP 5.0 - 75%]<br />[SCWCD 1.4 - 85%]<br />------------------<br />Tiger, Tiger burning bright,<br />Like a geek who works all night,<br />What new-fangled bit or byte,<br />Could ease the hacker's weary plight?
Loveleen Saroya
Ranch Hand

Joined: Aug 08, 2005
Posts: 42
Sandeep during counting you typed '3' three times wont it be three different objects created?
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
ok lovleen ..i have edited the code..
but now there is a problem.
that i have got 10 string created with no such option in your question..
so only doubt is that if there would be 2 different objects created in prinln statement of just one..

i need to confirm that.
Lyn Yang
Greenhorn

Joined: Sep 13, 2005
Posts: 12
My answer is also 10

String s1 = "spring"; ->+1
String s2 = s1+"summer"; ->+2
s1.concat("fall"); ->+2
s1.concat(s2); ->+1
s1+="winter"; -->+2
System.out.println(s1+" "+s2); ->+2
------------------------------------------
Total: 10 ??
Ritu Kapoor
Ranch Hand

Joined: Oct 03, 2004
Posts: 102
Well, I think the answer would be 9. Mine is totally different from all of you guys! I've found 9 objects


Code:
---------------------------------------------------------------------------
String s1 = "spring"; //1 object
String s2 = s1+"summer"; //2 objects i.e summer & springsummer
s1.concat("fall"); //2 objects i.e fall & springfall
s1.concat(s2); //1 object springspringsummer
s1+="winter"; //2 objects winter & springwinter
System.out.println(s1+" "+s2); //1 object springwinterspringsummer

-------------------------------------------------------------------------
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi Ritu,

That is what i have told in my previous post
only doubt is that if there would be 2 different objects created in prinln statement or just one..


one thing is not clear whether two objects are created in
System.out.println(s1+" "+s2); or just one

if compiler uses StringBuffer to concat such statements than there would be one object created in this line otherwise we can see it in this way:
object 1) s1+" "
object 2) object 1+ s2

that is first it will add a space to object s1 which would result in creation of a new object and then it would add s2 to the newly created object which would again create a new object hence 2 objects.

but if it uses StringBuffer then the result might be just 1 object at that line.

this is the only confusion.

Sandy
[ September 13, 2005: Message edited by: Sandeep Chhabra ]
saxena vicky
Greenhorn

Joined: Sep 05, 2005
Posts: 18
Guys I have a doubt
When I print the hash code of the string object
--------------------------------------------------------------------

String s1 = "spring";
System.out.println("--s1---"+System.identityHashCode(s1));
String s2 = s1+"summer";
System.out.println("--s2---"+System.identityHashCode(s2));
s1.concat("fall");
System.out.println("--s1---"+System.identityHashCode(s1));
s1.concat(s2);
System.out.println("--s1---"+System.identityHashCode(s1));
System.out.println("--s2---"+System.identityHashCode(s2));
s1+="winter";
System.out.println("--s1---"+System.identityHashCode(s1));
System.out.println(s1+" "+s2);
System.out.println("--s1---"+System.identityHashCode(s1));
System.out.println("--s2---"+System.identityHashCode(s2));
--------------------------------------------------------------------
OUTPUT
======
--s1---15655788
--s2---26533766
--s1---15655788
--s1---15655788
--s2---26533766
--s1---14613018
springwinter springsummer
--s1---14613018
--s2---26533766

can any one explain me why???
In that case answer will not be 9
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,




I have read that concat will give rise to an object when we are assigning the reference ...to a string variable

s1.concat("fall"); //here s1 value is "spring"
s1=s1.concat("fall"); //here s1 value is "spring fall"..

Now Whats the actual answer to the above code...

8 or 9 or 4.....

Regards
[ September 13, 2005: Message edited by: A Kumar ]
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hello A Kumar,
s1.concat() will always creat an object, whether you store the reference in variable or not..
so here at every concat atleast one new object is created..

so surely the answer will be either 9 or 10(a bit of confusion for the println statement)

Sandy
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
This example is there in K&B book with a good explanation as well.
And the answer to this is 8 .

Shivani.


/** Code speaks louder than words */
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,

Shivani...Can you post the explanation....



Regards
anand phulwani
Ranch Hand

Joined: Sep 10, 2005
Posts: 242
Shivani Please post the explination...
it would be helpful to all of us.

Thanx
Anand


Thanks and Regards, Anand
SCJP 5.0 310-055 73%, SCWCD 1.4 310-081 78%, IBM DB2 9 Fundamentals 000-730 62%
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Shivani we all were expecting some useful stuff from your side.
if you know the correct answer then its an humble requst that you let us all know about it.

kindly post the explination of this code if you have.

Thanx.

Sandy
Ramesh Ar
Greenhorn

Joined: Sep 13, 2005
Posts: 1
Hello Everyone,
Correct me if I am wrong. When you say new String("x") then two objects are created. One of them is placed on the heap and the other in the String pool (If not present). So when you iterate through the loop, new object is created everytime for new String("..") and another String object for new String("..") + i. So how does the result end up with 11?
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hello Mr Ramesh Ar,
Yes you are right that whenever you use new String("x") then two objects are created. One of them is placed on the heap and the other in the String pool (If not present).

So how does the result end up with 11?

well in the above mentioned question i dont find any for loop that would create new strings in loop nor do i find use of new String("...").

the answer that i have given , ie 9 or 10 is merely because of concatinaion of string and creating new string in pool.

could you be more specific about your doubt?

Sandy
Lalitha Vydyula
Ranch Hand

Joined: Jul 20, 2005
Posts: 69
Hi All,

I think Sandeep is right.here 8 objects are created in this code
String s1 = "spring";
String s2 = s1+"summer";
s1.concat("fall");
s1.concat(s2);
s1+="winter";
System.out.println(s1+" "+s2);

1) "spring" referenced by s1
2) "summer" in pool
3) "springsummer" referenced by s2
4) "fall" in pool
5) "springfall" in pool
6) "springspringsummer" in pool
7) "winter" in pool
8) "springwinter" referenced by s1, reference to spring is lost

So 8 objects and 2 references are created.

The answer would be 9 if
String s1 = new String("spring");

Let me know if I'm worng.

-Lalitha
Lalitha Vydyula
Ranch Hand

Joined: Jul 20, 2005
Posts: 69
Hi All,
This is the explaination from the book. Hope it will help you.....
String s1 = "spring ";
String s2 = s1 + "summer ";
s1.concat("fall ");
s2.concat(s1);
s1 += "winter ";
System.out.println(s1 + " " + s2);

how many String objects and how many reference variables were
created prior to the println statement?

Answer:
The result of this code fragment is �spring winter spring summer�.
There are two reference variables, s1 and s2. There were a total of eight String objects created as follows: �spring�, �summer � (lost),
�spring summer�, �fall�(lost), �spring fall� (lost), �spring summer spring� (lost), �winter� (lost), �spring winter� (at this point �spring� is lost). Only two of the eight String objects are not lost in this process.


-Lalitha
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi,
Thanx Lalitha you have done a great job.
i have said earlier that there may be either 9 or 10 objects created in the program but that also inludes object created in println statement. otherwise before println statement there are surely 8 objects and 2 references created.

but i m still not able to tell that exactly how many objects are creted in println statement 2 or 1.

can anyone throw some light on how does println will handle concatination of THREE string objects ,whether it will create two objects (that means it uses String for concatination) or just one(that meanse it uses StringBuffer for concatination) ?

Thanx

Sandy
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
And Loveleen if you have taken the code from the K&B book than you forgot to mention a very important line over there:

how many String objects and how many reference variables were
created prior to the println statement?

please recheck

although this missing sentence has made me learn a lot...thanx anyways.

Sandy
Lalitha Vydyula
Ranch Hand

Joined: Jul 20, 2005
Posts: 69
Hi Sandeep,
The Java language provides special support for the string concatentation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.


Hope this will help you.

-Lalitha
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Voila !!
We have got the answer.
so now we can surely say that answer is 9

Thanx again Lalitha so at last after 20 posts we can surely say that answer is going to 9 that would include the object created in prinln statement...

Thanx everyone

Sandy
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
oops. I was a little late to give the quoted text from K&B Book. I had assumed that everyone must be owning one .

cheers,
shivani
Lyn Yang
Greenhorn

Joined: Sep 13, 2005
Posts: 12
I think the answer should be 10. Because there is a " " in the println. That is is why I think the println statement has 2 string objects created.
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
Thanks Lalitha,

you have done a gr8 job nd frnds that was a very good discussion .


thanks a lot

srikanth


Thanks & Regards<br /> <br />-Srikanth
Dharmesh Gangani
Ranch Hand

Joined: Feb 20, 2004
Posts: 30
Hi,

I agree with Lyn Yang. The answer could very well be 10 as " " is present in the System.out.println() statement. This string will be created in the heap as there is no such string in the heap at that point.


-=-=-=-=-=-=-=-=-=-<br />Thanks & Regards,<br />Dharmesh G.
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hello Dharmesh Gangani and Lyn Yang,
well let me clear your doubt that why there will 9 objects created and not 10.
The Java language provides special support for the string concatentation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuffer class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java.


See friends as its quoted that String concatenation is implemented trhough the StringBuffer class and its append method then there is no question of having two objects.

hope you got the point.

Sandy
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Originally posted by Loveleen Saroya:
How many string objects are created in the following code:-



1)2
2)4
3)8
4)9
im confuse that in System.out.println statement 2 objects r created or 1.and in this line s1.concat("fall"); one or two String objects are created.



I am really sorry for having another doubt after so much of dicsussion and myself answering the question.

well the doubt is whether objects created in println statement will at all be counted as String objects? Since as we read that string Concatenation takes places using StringBuffer(and not String). And we have both the options that can be right(as far as my doubt is concerned)
so can anyone tell whether ans is 8 or 9including the println statement.
Remember question is:
How many string objects are created in the following code
???

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi sandeep

coming to the examination point of view..as i am also preparing for it ...

i think if the question is bout how many strings are created then its better to go for 8 as JLS suggests that println makes stringbuffer objects..and if the ques is how many objects are created ??then its suppose to be 9 thats for sure na ....

plz correct me if wrong

srikanth
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hi srikanth,

well i totally agree with you.

and had i got this question in my exam i think i would have selected the ans 8.

thanx everyone for your comments and imparting m such useful knowledge.

Thanx

Sandy
agrah upadhyay
Ranch Hand

Joined: Sep 01, 2005
Posts: 579
String s1 = "spring";
String s2 = s1+"summer";
s1.concat("fall");
s1.concat(s2);
s1+="winter";
System.out.println(s1+" "+s2);
**************************************************************
1)."spring"
2)."summer"
3)."springsummer" 4)."fall" 5)."springfall"
6)."springspringsummer" 7)."winter"
8)."springfallspringsummerwinter" 9)."springfallspringsummerwinterspringsummer"



Agrah Upadhyay
B.Tech
3rd Year
[ September 15, 2005: Message edited by: agrah upadhyay ]
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Hello Aghra
I think you need to read the whole topic again to get the better grasp of wats going on over here...

anyways...the objects you have told are wrong(i far as i think)

the objects that will be creted are:
1) "spring" in pool referenced by s1
2) "summer" in pool
3) "springsummer" (s1+"summer") referenced by s2
4) "fall" in pool
5) "springfall" (s1.concat("fall")) object created although not stored in any variable
6) "springspringsummer" (s1.concat(s2))object created although not stored in any variable
7) "winter" in pool
8) "springwinter" (s1+="winter")referenced by s1

and in the println statement there will be ONE StringBuffer Object containing "springwinter springsummer".

Sandy
Chris Delano
Greenhorn

Joined: Jan 20, 2004
Posts: 6
My question would be:

How is the StringBuffer object in the println statement created?

I'm guessing that it would be something like the following:

System.out.println(new StringBuffer(s1).append(" ").append(s2).toString());

If so, I would assume that two Strings are created in this line. One being the space in the second append method and the other being the String returned by the StringBuffer's toString() method call.

Chris
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340

Hello Friendz,

This innocent looking question is getting worse then worst.
even after 30 posts we have not yet reached to the answer.
everytime we think that we have got the answer, some or the other reason take us back from where we started.

I HIGHLY SUGGEST SOME HIGHLIY EXPERIENCED PERSON SHOULD MAKE THE FINAL COMMENT ON THIS QUESTION.

Sandy
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
Wow! What Fun!

Just FYI, in the book the question we ask is "How many String objects and how many reference variables were created prior to the println statement."

Does that help?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Sandeep Chhabra
Ranch Hand

Joined: Aug 28, 2005
Posts: 340
Truely said Mr. Bert

And for that we also have got the answer.
but we need know what the hell is going in the println statement.
please help us regarding that statement.

Sandy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how many String objects are created