Thursday, September 29, 2016

String in Java (Why String is Immutable in Java)

String, which are widely used in any programming language as Java. String is a sequence of characters. In Java Strings are Object. As same as other object Strings instance will be created by new keyword, as follow:

     String s = new String();

This line of code will create new object of String and assign it to reference object “s”. String seems just like other object. Now let’s assign value to it:

     s = "Hello Java";

There are so many construction available for the String object, more efficient will be

     String s = new String("Hello Java");

And below is the more concise

     String s = "Hello Java";

String are Immutable Object


Once you assign value to String, that value can never been change – it’s Immutable. But although String is immutable, its reference variable is not. Confuse let see example, consider this using same above reference:

     s.concat("World"); //Append value "World" at the end of string "s"

Java Virtual Machine(JVM) took value of s (which was “Hello Java” ) and append it with “World” at the end of string “s”, end return as “Hello Java World”.

If you try to print value of reference variable value is still “Hello Java”. We have applied concat function on string but still value is not changed, so why this behaviour?

Since String is immutable JVM shouldn’t append this new string to old one, instead it created a new string “Hello Java World”. If you want to assign this new value to old reference variable you need to assign it back to “s” as shown below:

     s = s.concat("World");

Now reference variable s is contain value “Hello Java World”.

String is store in Heap but there is special segment or part assign to call “String Pool”, where all String value is reside. As shown in below figure:



As shown in figure there are total three string is created. Initially reference “s” is refer to string “Hello Java” in string pool.

After concat is function is applied to “s”, the original unchanged string containing “Hello Java” would still exist in memory, but it would be consider as “Lost”. No code in our program has any way to refer it – it is lost to us. 

Keep in mind, that the original string “Hello Java” didn’t changed, only the reference variable “s” was changed so that it would refer to a different new string in string pool, as shown in below figure (old reference is indicate with dotted line)

To make Java more memory efficient the JVM set this String Pool concept. When compiler encounter a String literal, it check the pool to see if an identical String is already exist. If match is found, the reference to new literal is directed to the existing String, and no new String literal object is created.

Now you can see that why making String object immutable is such a good idea. If several reference variable refer to the same String without even knowing it, it would be very bad if any of them could change String’s value.

You might think that what if someone override String class functionality, couldn’t that cause problem in pool? That’s one of the main reason why String class is marked final. Nobody can override the behaviour of any of the String methods, so you can assured that the String object to be immutable.


Important methods in String class

  • ·        charAt() – Return character located at the specified index
  • ·        concat() – Append one String to the end of another
  • ·        equalsIgnoreCase() – Determine the equality of the two string, ignoring case
  • ·        length() – Return the number of character in String
  • ·        replace() – Replace occurrences of a character with new character
  • ·        substring() – Return part of the String
  • ·        toLowerCase() – Return a String, with uppercase characters converted to lowercase
  • ·        toString() – Return the value of String
  • ·        toUpperCase() – Return a String, with lowercase characters converted to uppercase
  • ·        trim() – Removes whitespace from both ends of a string