1. We can store string in two different spaces.
    • String Pool for String Literal :
      It is special storage in Java Heap. When a string is created and if the string already exists in the pool, the reference of the existing string will be returned, instead of creating a new object and returning its reference. Below both strings are same and pointing to the same object in String Pool: You can used relational equality operator ‘==’ to compare references of two strings in above case. String is immutable, it is not efficient to use String if you need to modify your String frequently (that would create many new Strings occupying new storage areas).
    • Heap Space for String Object :
      Every String object in the heap has its own storage like any other object. There is no sharing of storage in a heap even if two String objects have the same contents. We have to use equals method to compare string. It is uncommon and not recommended to use the new operator to construct a String object in the heap.
  2. We can cache hashcode of string. As string is immutable and value of string in pool is not going to change. Same logic applies to hashmap, as hashmap doesn’t require to calculate it’s key hashcode every time.
  3. Strings are implicitly thread safe. Since String is immutable we do not require to synchronize objects if it is shared between multiple threads.
  4. String prevents security threats. Strings are used in many applications i.e. database connection, file opening, network connections etc. If string is not immutable, someone can change the value of String and change the host URL and password and gain access to the system. As java.lang.String is final no one can override String Class.
  5. String are useful in loading classes. For example if you are trying to load java.sql.Connection using driver of oracle and someone change its value to mysql driver which can do unwanted things.
It's good to share...Share on FacebookTweet about this on TwitterShare on LinkedInPin on PinterestShare on Google+Email this to someone

6 Thoughts on “Why is String immutable in Java?”

  • Hello sir,i have a little doubt with your explanation.
    Here the question is why String is immutable in java?

    as the 1st point you have write string pool.
    i.e. it explain because to implement string pooling concept they have made string as immutable.
    But i think as String is immutable ,that’s why they have create the concept of string pooling as it will be helpful.
    Your explain line and my line are totally different.I mean all wrapper class for e.g. Integer is also immutable.But it does not have any Integer pool concept.
    There must be other reason why a class is immutable.

    • Hello Subhrasu,

      Thanks for sparing your time in reading. We are always here to answer and help people like you.

      Answer to your query:
      Why all wrapper classes are not immutable?
      [Answer] There are multiple reasons:
      1. String is most widely used data type compare to others.
      2. Numbers are easy to store in heap than pool. Because immediate opcode value.
      3. In recent Java versions, Byte, Short, and Integer objects from -128 to 127 (0 to 127 for Character) are precached for performance reasons, not to save memory. It will cover a large number of common uses, while it would be impractical to try to precache a very large number of values.

      Thanks,
      codeNuclear Team

Leave a Reply

Your email address will not be published. Required fields are marked *