This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes String inNutShell Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "String inNutShell" Watch "String inNutShell" New topic
Author

String inNutShell

Amit Prakash Singh
Ranch Hand

Joined: May 11, 2006
Posts: 38
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


Amit Prakash Singh<br /> <br />SCJP 1.4 93% <br />SCWCD1.5 85%
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: String inNutShell