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 String Objects 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 "String Objects" Watch "String Objects" New topic
Author

String Objects

Anurag Blore
Ranch Hand

Joined: Jan 15, 2003
Posts: 74

My answer is
String s = "Amar"; //First Object
s = s + "50";//Second Object
s = s.substring(2, 5);//Third Object
s = s.toUpperCase();//Four Object.

But I am not sure, Can anyone put his comment.

Thanks


Thanks,
Anurag
SCJP 1.2 & 1.5, PMP
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
String s = "Amar"; A new string object is created in String pool "Amar" and is assigned to s. So count is 1.

s = s + "50"; Now a new string object "50" is created in String pool. So right now there are two string objects "Amar" and "50". Now because of + a new String object is created "Amar50" and is assigned to s. So after this statement there are 3 string objects. "Amar", "50" and "Amar50"

s = s.substring(2, 5); This will again create a new string object that has "ar5" and assing it to s. So now we have 4 string objects "Amar", "50", "Amar50" and "ar5"

s = s.toUpperCase(); This will again create a new String object "AR5" and assign it to s. So now we have 5 string obects "Amar", "50", "Amar50", "ar5" and "AR5"

return s.toString(); this will simply return s. and hence no string objects are created here.
So total result is 5 "Amar", "50", "Amar50", "ar5" and "AR5".


SCJP, SCWCD, SCBCD
Anurag Blore
Ranch Hand

Joined: Jan 15, 2003
Posts: 74
Thanks Deepak,

Do you think in Java If there is a way we can write a program to check the
number of Objects created.

I am asking this question as this will be helpful for answering questions
for scjp exam.
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
For Strings or other built-in clases, i dont think so you can do this. But for user defined clases you can do this by creating a static int counter and incrementing the counter in the constructor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: String Objects
 
Similar Threads
String Objects...
how many String objects created??
How many String objects will be created???
String Test
doubt with string abjects