Saturday, July 15, 2006

Why String class is immutable?

In this blog i am writing about the immutable String class in java. The reason for making it as immutable object.

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.

6 comments:

Nasir said...

Hi

What ever you said about string class of maintaining the pool and whenever we create a new string object it will refer in the pool is some what ambiguous.

What i know is that

We can create strings by two ways

String str1 = new String("Hello") and
String str2 = "Hello"

Actually these two statements differ among themselves only in the manner how they are organized.

Now when ever we create a string objecct without new keyword (str2) then JVM will first refer the string pool that is maintained by it for the same string by comparing with each other. If it matches then reference will automatically point to this place.
Suppose if we create a string object with new keyword (str1) then JVM will never check the string pool and just it creates a new object in the heap and the reference will be pointing to this place.

That is what i know.
Please correct me if I am wrong.

One more thing to mention is we can make a string that is created with new keyword to be places in string pool with methoc intern() of string class.

Nasir said...

Hi

What ever you said about string class of maintaining the pool and whenever we create a new string object it will refer in the pool is some what ambiguous.

What i know is that

We can create strings by two ways

String str1 = new String("Hello") and
String str2 = "Hello"

Actually these two statements differ among themselves only in the manner how they are organized.

Now when ever we create a string objecct without new keyword (str2) then JVM will first refer the string pool that is maintained by it for the same string by comparing with each other. If it matches then reference will automatically point to this place.
Suppose if we create a string object with new keyword (str1) then JVM will never check the string pool and just it creates a new object in the heap and the reference will be pointing to this place.

That is what i know.
Please correct me if I am wrong.

One more thing to mention is we can make a string that is created with new keyword to be places in string pool with methoc intern() of string class.

Krishna melkote said...

Hi, as far as I know, only "literal" strings go to string pool (also the interned strings), however, I have no idea as to how the JVM garbage collects such strings in the pool. You have any idea as to where the string pool is placed ? whether the JVM heap or the native memory ?

sravan said...

Hi

Could you tell me. String class is immutable. If i want to do userdefined class also immutable class if it is possible how?

Unknown said...

Making String class as a immutable must be a big decision for java people. Only to create a string pool to optimize the processing does not define why the string class is immutable. For a very little efficiency they had contradicting the basic concepts of oops, inheritance. There are many other reason which are more probable than string pool answer. For example, if string is stored as a key in map, then nobody shall able to change the value of key. The string is immutable, you can't change the value. This could deifne one purpose of string being immutable.

Unknown said...

Nishant,

Not only that the main reason to keep all the Wrapper class like Integer, Float, Double, String as immutable is for the reason of Java Security. Let assume String is Mutable the the Java Security is Zero.

Have you people thought the reason of having String as the parameter to get hold of any resources like File, Database or any resource? why it is not StringBuffer?

Lets say if you want to get the access of one remote file, you just send the filename to the access sytem which authenticates you, once you got access your should never change the file or any resource location so that is the main reason for having all wrapper class as immutable...