• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Immutable in java

 
yogesh m gaikwad
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

what is mean by string is mutable
can you please explain with example
 
Winston Gutkowski
Bartender
Pie
Posts: 10508
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yogesh m gaikwad wrote:what is mean by string is mutable
can you please explain with example

I think you mean "String is immutable", but here goes:Try it out, and see what it does.

Winston
 
Ankush Kaundal
Ranch Hand
Posts: 36
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strings are always immutable which means once created it can't be changed instead a new String object will be created.
 
Narayan Nghalli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since String is immutable it can safely be shared between many threads, by which we avoid synchronization issues.

We can store string in string pool for example as below.If it was mutable then this would not be possible.

String immutability allows to cache its hash value which greatly improves String performance in Hash based collections.

Better memory management –
Consider
String s1=”hello world”;
String s2=”hello world”;

In the above case jvm automatically points to same address in heap memory.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic