permaculture playing cards
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

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
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: 8663

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.


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

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:
subject: Immutable in java
It's not a secret anymore!