• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how many String objects are created

 
Loveleen Saroya
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Loveleen Saroya
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandeep during counting you typed '3' three times wont it be three different objects created?
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This example is there in K&B book with a good explanation as well.
And the answer to this is 8 .

Shivani.
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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



Regards
 
anand phulwani
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shivani Please post the explination...
it would be helpful to all of us.

Thanx
Anand
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 380
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Lalitha,

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


thanks a lot

srikanth
 
Dharmesh Gangani
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 579
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic