Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

pls clear the concept of creating new String

 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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".
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic