Introduction In Java, string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object. The Java String is immutable which means it cannot be changed. Whenever we change any string, a new instance is created. For mutable strings, you can use StringBuffer and StringBuilder classes.
String Constant Pool(SCP) A string constant pool is a separate place within the heap memory where the values of all the strings which are defined are stored. When we declare a string, an object of type String is created in the stack, while an instance with the value of the string is created in the heap(SCP in this case). On standard assignment of a value to a string variable, the variable is allocated stack, while the value is stored in the heap in the string constant pool.
Ways to create a String object? In Java, there are two ways to create String object:
By string literal - Java String literal is created by using double quotes. For example: String a = "Hello World". Each time we create a string literal, the JVM checks the string constant pool[SCP] first. If the string already exists in the pool, a reference to the pooled instance is returned. If the string doesn't exist in the pool, a new string instance is created and placed in the pool. In the below example, the first time the JVM will search for an object in the SCP with value "Hello World" and being unable to find that, will create a new object in SCP. Second time wile creating String b, the JVM finds the value "Hello World" in the SCP and returns the reference of the object initially created in the SCP. It doesn't create a new object in SCP the second time for the same value of the object. NOTE: This way it becomes more space efficient as no duplicate objects are created in the SCP.
String a = "Hello World"
String b = "Hello World"
By new keyword - String object with new keyword is created as follows: String a = new String("Hello World"). While creating a String object with a new keyword, we create an object in the SCP and an object in the Heap memory. The new String instance created points to the object in the Heap memory. As we are creating with a new keyword, the String object is created at run-time and hence if another string object is defined with the same value, no new object is created in the SCP, but a new object is created in the Heap memory.
Why is String immutable? When a String variable is defined having the same value as another string variable, they both point to the same object in the SCP. If String was made mutable, then changing the value of one instance, will result in changing the value of the String object in SCP, which is referenced by other instances as well. This might lead to problem in the rest of the instances, which were never meant to receive the updated value. Hence String was made immutable to restrict unnecessary mutations.
How to create mutable String object? There are two ways to create mutable String object:
StringBuilder - Java StringBuilder class is used to create mutable (modifiable) string. StringBuilder s = new StringBuilder("Hello World") The StringBuilder class in java is same as String class except it is mutable i.e. it can be changed. StringBuilder stores the object in heap which allows it to be modified. StringBuilder is also not thread safe[multiple threads can work on it simultaneously] and is unsynchronized. StringBuilder is fast as it is not thread safe .
StringBuffer - Java StringBuffer class is used in the same manner as StringBuilder, but it is synchronized and thread safe. Being synchronized makes it slow.
Every time the string object is mutated using StringBuffer/StringBuilder, the object initially created in the Heap space is updated. In case of String, if we try to mutate the String variable, a new object is created in the Heap memory everytime for the updated value of the string.
Summary In this post, we have had an introduction to String in JAVA and the different ways to create it. Also we have seen how to create mutable String and the internal mechanism for immutable and mutable String Objects.
Comments