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
posted 11 years ago
"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();
t = t.toUpperCase(); // in this t refers now to the upcased string, and s still refers to the original string "fred".