File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many String are created?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many String are created??" Watch "How many String are created??" New topic
Author

How many String are created??

Sabrina Biccari
Greenhorn

Joined: Feb 15, 2008
Posts: 4
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

Joined: Nov 27, 2007
Posts: 108
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


<i>If there were no Exceptions (not Errors!), Life would have kinda sucked!!</i>
Sabrina Biccari
Greenhorn

Joined: Feb 15, 2008
Posts: 4
Thanks Nadeem,
so I have six String created and the StringBuffer is not created.!!!
Sabrina Biccari
Greenhorn

Joined: Feb 15, 2008
Posts: 4
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

Joined: Oct 22, 2007
Posts: 144
Hi Sabrina,
My guess is that there would be 7 String objects here.




[ February 15, 2008: Message edited by: Jart Bo ]

SCJP, SCWCD
Ali Khalfan
Ranch Hand

Joined: Nov 03, 2007
Posts: 126


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

Joined: Oct 22, 2007
Posts: 144
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

Joined: Dec 29, 2007
Posts: 129
Somebody please clarify
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
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

Joined: Sep 25, 2007
Posts: 156


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


Abhijit Das
SCJP 5.0 | SCWCD 1.5
Nadeem Khan
Ranch Hand

Joined: Nov 27, 2007
Posts: 108
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many String are created??
 
Similar Threads
How many Object is created?
How many objects are created?
Difference between String
Stings: how many objects created?
String concatenation