Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How many String are created??

 
Sabrina Biccari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm sabrina ..I have doubts about: How many String this code creates and where (pool Memory or no pool Memory)

String s = new String("Java");
StringBuffer sb = new StringBuffer();
sb.append(s + " " + "is" + " " + "cool");
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, here what i "think":

String s = new String("Java");
------If this means the same as following:
String s = new String();//A String got created in non-pool memory!
s = "java"; // A string "java" created in pool memory!

StringBuffer sb = new StringBuffer();
// No string created!

sb.append(s + " " + "is" + " " + "cool");
// "is" in pool
//"cool" in pool
//" " in pool
//"java is cool" in pool //and that rhymes
 
Sabrina Biccari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nadeem,
so I have six String created and the StringBuffer is not created.!!!
 
Sabrina Biccari
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,
so I have six String created and an StringBuffer.

If I have another code:

StringBuffer sb = new StringBuffer("java");

I have only an StringBuffer created or I have an StringBuffer created and an String (java) created in poolMemoory??
 
Jart Bo
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sabrina,
My guess is that there would be 7 String objects here.




[ February 15, 2008: Message edited by: Jart Bo ]
 
Ali Khalfan
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


you mean 8.

2 in the first statement, 1 in the second, and 5 in the third

2+1+5 = 8
 
Jart Bo
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ali,

I was thinking the 2nd line creates a StringBuffer object, not a String. Please correct me if I'm wrong. Thanks!
 
Dean Jones
Ranch Hand
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Somebody please clarify
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sabrina Biccari:

String s = new String("Java"); //2
StringBuffer sb = new StringBuffer(); //0
sb.append(s + " " + "is" + " " + "cool"); //5


I would say 7 String objects created.

If you wanna be tricky, the part I'm not sure is how the strings on the 3rd line are concatenated:


" " //1
"is" //2
" " //3
"cool" //4
"Java is cool" //5

OR

" " //1
"is" //2
" " //3
"cool" //4
"java " //5
"java is" //6
"java is " //7
"Java is cool" //8

Alex
[ February 18, 2008: Message edited by: Alex Belisle Turcot ]
 
Abhijit Das
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


All are saying that the above line creates two objects. One object is "JAVA" referred by 's' and what is the other object and reference variable to refer it.
thanks
 
Nadeem Khan
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = new String("JAVA");

What i inferred from K & B, it creates two objects - "java" in pool memory and another object (new String()) in non-pool.And s points to "java".

Can someone please explain the relation between these two objects getting created here?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic