• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String inNutShell

 
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.

Example:

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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic