• Post Reply Bookmark Topic Watch Topic
  • New Topic

why String class is immutable?  RSS feed

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why javasoft need to keep Sting class immutable?
and why StringBuffer is mutable?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Java designers recognized that many many security holes are created by sloppy handling of strings. Therefore they made modification of an existing string variable impossible. This also has other advantages - for example the intern() method saves storage space.
Bill
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and given that String is immutable, the reason for the existence of StringBuffer should be obvious -- sometimes you want to be able to alter a sequence of characters.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!