aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings concat() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings concat() " Watch "Strings concat() " New topic
Author

Strings concat()

Ivan Ivanoff
Ranch Hand

Joined: Jan 04, 2002
Posts: 56
dear ranchers,
String s = "please help"; //1
s.concat(" me!"); //2
System.out.print(s); //3
Why it prints "please help" without " me!" ?
I kind of know that if I change line //2 to be like s = s.concat(" me!") then it will create brand new String object & output will be
"please help me!".
So what is the reason to use
conctat() method without creating brand new String object like i have on line //2 ???
Any help would we great !
thx
Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
Ivan,
Because strings are immutable, a new string is created everytime we try to modify it.So in your case s.concat("me") creates a new string object and s still remains the same.
Hope this helps
Kareem
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings concat()