aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes pls clear the concept of creating new String 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 "pls clear the concept of creating new String " Watch "pls clear the concept of creating new String " New topic
Author

pls clear the concept of creating new String

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
Hi,
can any body please tell me that in code

String s = "fred";
String t= s;
t.toUpperCase();

after line to both s and t refer to same string object as string object can't be changed and alteration to it by t object will create new string object.
In above case, when t.toUpperCase() in invoked new string object is made and assinged to t ?? is i am right ?

actually in kethy sera/bate book i m getting confustion that they have wrote that string will be "abandoned" coz it is not assinged to any string varible ?

please clear the above fundat in details... will be gladfull


thanx and regards,
amit


Thanks and Regards, Amit Taneja
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
"In above case, when t.toUpperCase() in invoked new string object is made and assinged to t ?? is i am right ?"

No. A new string object will indeed be created but it will have no reference to it. so it will be eligible for garbage collection immediately.

What you need to do is to assign the new string to a String reference:

String upper = t.toUpperCase();

Or;

t = t.toUpperCase(); // in this t refers now to the upcased string, and s still refers to the original string "fred".


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pls clear the concept of creating new String