jump to navigation

Why String class is immutable? August 16, 2006

Posted by Allu in JAVA.
trackback

JVM internally maintains the “String Pool”. To achive the memory
efficiency, JVM will refer the String object from pool. It will not
create the new String objects. So, whenevr you create a new string
literal, JVM will check in the pool whether it already exists or not.
If already present in the pool, just give the reference to the same
object or create the new object in the pool. There will be many
references point to the same String objects, if someone changes the
value, it will affect all the references. So, sun decided to make it
immutable.

Advertisements

Comments»

1. kumar - December 14, 2007

Can you explain why the String class is final?

2. Tirthi - February 2, 2012

can u please explain with some more solid reasons with some real time examples why string is immutable in java?

gorityala ramana - September 25, 2012

SUB CLASS IMPLEMENT THE SUPER CLASS (String) method ,String methods are overridden,to avoid this problem string is declared as final


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: