aspose file tools*
The moose likes Beginning Java and the fly likes Immutable in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Immutable in java" Watch "Immutable in java" New topic
Author

Immutable in java

yogesh m gaikwad
Greenhorn

Joined: Dec 28, 2011
Posts: 12

what is mean by string is mutable
can you please explain with example
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8186
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Ankush Kaundal
Ranch Hand

Joined: May 12, 2011
Posts: 36

Strings are always immutable which means once created it can't be changed instead a new String object will be created.
Narayan Nghalli
Greenhorn

Joined: Apr 25, 2012
Posts: 3
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.
 
Consider Paul's rocket mass heater.
 
subject: Immutable in java