This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
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()
It's not a secret anymore!