jQuery in Action, 3rd edition
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

Immutable in java

yogesh m gaikwad

Joined: Dec 28, 2011
Posts: 12

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

Joined: Mar 17, 2011
Posts: 8942

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.


Bats fly at night, 'cause they aren't we. And if we tried, we'd hit a tree -- Ogden Nash (or should've been).
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

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 –
String s1=”hello world”;
String s2=”hello world”;

In the above case jvm automatically points to same address in heap memory.
I agree. Here's the link: http://aspose.com/file-tools
subject: Immutable in java
It's not a secret anymore!