File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings concat() " Watch "Strings concat() " New topic

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 !
Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102
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
I agree. Here's the link:
subject: Strings concat()
jQuery in Action, 3rd edition