Jul 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.

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

5 Comments on this post

Trackbacks

  1. 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.

    October 4th, 2006 at 8:40 pm
  2. krishna 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 ?

    August 8th, 2007 at 5:45 am
  3. 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?

    January 16th, 2008 at 10:14 pm
  4. Nishant 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.

    June 11th, 2008 at 5:25 am
  5. Meher said:

    I too agree with you Nishant. For a little efficient, they are contradicting the concepts of oops, inheritance.

    If we create an object using,
    String str1 = new String(“Hello”)
    2 objects will be created one in string constant pool, the other one in normal memory(heap) where all the objects are stored.

    But, if we use..
    String str2 = “Hello”
    one literal will be created in string constant pool. All the remaining references point to the same string literal.

    September 30th, 2009 at 1:42 am

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

Free E-Mail Subscription

    Enter your email address:

    Delivered by FeedBurner

Featured Post

Categories

Finance Blogs