• Post Reply Bookmark Topic Watch Topic
  • New Topic

Replace property in String  RSS feed

 
Rajesh Santhanakrishnan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,




In the above code , i m getting the output as

the value of a before replace test
the value of a after replace test

I dont get why the replaced value is not getting printed.

Could anyone help me on this

Thanks
Navaneethan
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are immutable. Your call to a.replace(...) does not modify a; it creates a new String. You'll need to assign that to some variable (like a):
 
Rajesh Santhanakrishnan
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob

Is StringBuffer is also immutable?

Thanks
Navaneethan
 
Soumyajit Hazra
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through java doc for String,StringBuffer and StringBuilder. Try the same code with StringBuffer to get the answer.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use StringBuffer, but StringBuilder, for most applications.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!