1
Sponsored Links


Ad by Google
Very very interesting and very popular java interview question and of course one of my favorite java interview question. In every java interview definitely you will face Why String is Immutable in Java, Or can you list down few secret of String? Or Why String is final? even if you are going for an architect level position also. Few java interview questions are ever green like Comparator Vs Comparable, Design your own ArrayList, Which Collection is better to store 10 millions of records between ArrayList and LinkedList.

OK OK, come to the point of this post Why String is Immutable in Java?
Well, There are lot of advantages of String class being an Immutable and below are the five key points.

Five main reason Why String is Immutable

  1. String Constant Pool
  2. Security
  3. Synchronization
  4. Class loading
  5. Efficiency
String Constant Pool:
String constant pool is a virtual storage area inside the heap memory, for storing only one copy of each distinct String value, which must be immutable.
String pool allows you to save memory by keeping the immutable string into a pool, so that String can be reuse by multiple instances instead of creating a new one with the same value.
For example,
String softDrink = "Pepsi";
String mySoftDrink = "Pepsi";
We have two variable softDrink and mySoftDrink both have the same value as "Pepsi", There will be only one object created inside the String Pool. What happens internally, whenever any String is created, It will first trying to find the value of string inside the String pool, and if found returned the reference of that string from the pool instead of creating a new object. So in our case softDrink = "Pepsi" will goes inside the String pool and for mySoftDrink the reference of "Pepsi" will return instead of creating a new "Pepsi" object. See the image of String Constant Pool for both variables.

If String is not immutable, And, someone somehow change the softDrink value "Vat69" instead of "Pepsi" than mySoftDrink also get the changed value, and you know what happen when one get something without knowing. Of course I can dance like no one can. In short Sharing of string is not possible without String immutability.
Note: String constant pool, makes String processing activity more time and space efficient.

Security: The second main reason of String immutability is Security, Mostly parameters are used as a String value, while playing with network connection, passing user name, password, url etc while playing with database connection, reading a file from the file systems by passing file path as a String. If String were not immutable, than these parameters could be easily changed once authentication is done and that will caused the serious security threat.

Synchronization: String immutability automatically makes String as thread safe. So no need to worry about the synchronization issue while playing with String objects. Due to unchangeability of String, You can share String object into multiple threads without worrying about thread safety.

Class Loading: In java, class loader, String is used as an argument for class loading. So if string is not immutable than any one can change the name of class to be loaded by Class loader and that will again caused the serious security threat.

Efficiency: String is best candidate key for hashing mechanism for example String as a key in HashMap. The hashcode of string is cached at the time of creation and string is immutable so no need to calculate the hashcode again and again. This makes more and more time and space performance efficiency.

Friends, Above few points will really makes you more confident in a interview ground, and I am damn sure your interviewer get much and much convenience about Why String is immutable in java.
Sponsored Links

1 comments:

  1. very nice article on String.....Hats off sir, this site is really a helping hand for students like me

    ReplyDelete