A string is a very important topic in the world of programming languages. In almost all the programming languages such as C, C++, Java, Python, or in any application whether it is a Java-based or Python based, the most commonly used object is a string only.

For example, when you visit a bank to open an account then you are asked for information such as name, an address like house number, street, city, state, identification marks.

The information you provided is in the form of an ordered sequence of characters and symbols. This ordered and sequence of characters and symbols is nothing but a string in java.

So, if you are not familiar with a complete behavior of string, you are wasting your time to go to any technical interview because the string is the most favorite topic for the interviewer. Without making a command on the string you cannot crack any technical interview. So, let's understand in detail.

What is String in Java?

Generally, String is a sequence of characters. But in Java, String is an object that represents a sequence of characters. For example, "Pencil" is a string of 6 characters. 

String class is used to create a string object. String class is an immutable class which is present in java.lang package. But in Java, all classes are also considered as a data type. So, you can also consider a string as a data type. Immutable means it cannot be changed.

Basically, memory in Java is divided into three parts such as heap, stack, and String Pool. The string is so important in Java that it has dedicated memory location. What string pool does it?

Whenever a string gets repeated it does not allocate the new memory for that string. It uses the same string by pointing a reference to it for saving memory.

String Declaration in Java

The declaration of string class in java is as given below.
     public final class String 
          extends Object
          implements Serializable, Comparable<String>, CharSequence

which says that string is a final class and object class is its superclass. The class implements three interfaces Serializable, Comparable, and CharSequence.

The CharSequence is an interface which is used to represent the sequence of characters. String, StringBuffer, and StringBuilder class implement it. 

How to create a string object in Java?

To handle the string data in Java, we need the objects of the string class. Basically, there are three ways to create a string object in Java.
1. By string literal.
2. By new keyword.
3. By converting character arrays into strings

String literal in Java

String literal in Java is created by using double quotes.
For example:
            String s ="Hello"; 

The string literal is always created in the string constant pool. In Java, String constant pool is a special area which is used for storing string objects. Internally, the string class uses a string constant pool. This SCP area is the part of the method area (Permgen) until Java 1.6 version.

From Java 1.7 onwards, SCP area is moved in the heap memory because SCP is the fixed size in the method area but in the heap memory, SCP can be expandable. Therefore, the string constant pool has been moved to the heap area for memory utilization only.

Whenever you create a string literal, the JVM checks the string constant pool first. If the string already exists in the string constant pool, no new string object will be created in the string pool by the JVM.

The JVM uses the same string object by pointing a reference to it to save memory. But if the string does not exist in the string pool, JVM creates a new string object and placed in the pool. 
For example:
          String s1="Hello";
          String s2="Hello";
They have been represented in the memory as below given figure.
String literal in Java

In the above example, when the JVM will execute the first statement, it will not find any string with the value "Hello" in the string constant pool. So, it will create an object in string pool and stores the string "Hello" in that object as shown in the figure.

This object is referenced by the variable s1. In other words, if literal is not available in the pool, a new object will be created in the pool and the address of that object will be assigned to a reference variable s1.

When JVM will execute the second statement, it will first check the string constant pool to know whether the object with the same content is already available there or not. Since string with the value "Hello" is already available in the string pool.

So, JVM will not create a new string object in the pool and address of the available object will assign to reference variable s2. i.e it will point a reference variable s2 to the old existing object as shown in the figure.

Key points:
1. Remember that creating an object means allocating memory for storing data.
2. A string object is created for every string literal.

By new Keyword

The second way of creating an object to string class is by using the new operator. It is just like creating an object of any class. It can be declared as follows:
            String s=new String("Hello");

Whenever we will create an object to the string class using the new operator, In this case, JVM will create two objects, First, it will create an object in the heap area and stores the string "Hello" into the object and will point a reference variable s to the object in the heap. 

It can be seen in the below figure.
String constant pool in Java
Now JVM will create the second object as a copy for literal "Hello" in string constant pool for the future purpose. There is no explicit reference variable pointing to the copy object in the pool but internally, JVM maintains the reference variable implicitly.

Remember that the object created in the SCP area is not eligible for garbage collection because implicitly, the reference variable will be maintained by JVM itself.

Key points: 
For every string literal, one copy will be created in the SCP area.

By converting Character Arrays into Strings

The third way to create strings is by converting the character arrays into strings. Let's take a character type array: arr[ ] with some characters as given below.
           char arr[ ]={'j','a','v','a'};

Now create a string object by passing the array name to it like this:
          String s=new String(arr);

Now the string object 's' contains the string "java". It means that all the characters of the array are copied into the string. If you do not want all the characters of the array into a string then you can also mention which character you need, like this:
          String s=new String(arr, 1,3);

From the above statement, The total three characters will be copied into the string s. Since the original characters are j-a-v-a. So, the counting will start from 0 i.e the 0th character in the array is 'j' and the first character is 'a'. Starting from 'a', a total of three characters 'aja' will copy into the string s.

How many total objects will be created in the memory for the following string objects?

Here, we are creating four string objects using two new operators and 2 string literal. 
   String s1=new String("Scientech");
   String s2=new String("Scientech");
   String s3="Scientech";
   String s4="Scientech";

So, you tell me how many objects will be created in the heap and SCP area? 
Let's understand it by the below figure.
String constant pool in Java
1. During the execution of the first statement using the new operator, JVM will create two objects, one with content in heap area and another as a copy for literal "Scientech" in the SCP area for future purpose as shown in the figure. The reference variable s1 is pointing to the first object in the heap area. 

2. When the second statement will be executed, for every new operation, JVM will create again one new object with content "Scientech" in the heap area.

But in the SCP area, no new object for literal will be created by JVM because it is already available in the SCP area. The s2 is pointing to the object in the heap area as shown in the figure.

3. During the execution of third and fourth of statements, JVM will not create a new object with content "Scientech" in the SCP area because it is already available in string constant pool. It simply points the reference variables s3 and s4 to the same object in the SCP. They are shown in the above figure.

Thus, the total of three objects is created, two in the heap area and one in string constant pool. This kind of questions is always asked in the interview or technical test.

Final words 
Hope that this tutorial has covered the basic of string in Java, string literal in Java, and string constant pool in Java with memory concept. We hope that you will have enjoyed this tutorial. Thanks for reading!
Next ➤ Immutable String in Java ⏪ Prev Next ⏩