Techhgeekz ™

Tuesday 29 October 2013

why the string is immutable?

String pool (String intern pool) is a special storage area 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.

The following code will create only one string object in the heap.
String string1 = "abcd";
String string2 = "abcd";