Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

String inNutShell  RSS feed

Amit Prakash Singh
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jerry...
here is how Strings works:

whenever you apply any method on string objects, compiler automatically generates a new copy of orgional string objects, performs operation on new one and then returns the modiifed string objects. old one is not touched. so its up to you if you want to assign the reutrned object a reference or not.


String s = "XYZ"; //1. a object created

s.concat("ABC"); //now a fresh copy of XYZ created and "ABC" to it and the
//reference to XYZABC returned, but it get lost
//as you are not assiging reference to it.
System.out.println(s); // its "XYZ"

now lets see if you write:

System.out.println(s.concat("ABC")); //it will print XYZABC but the s=XYZ

if you write:
s = s.concat("ABC"); //then s=XYZABC
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!