We use string literals to create objects of type String.
If we surround one or more characters in a double quotation like ” “, we’re creating a string literal.
Note: even using an empty double quotation like “” represents string literal but an empty one.
Basically, there are two ways of creating a String object:
- Via constructors of the String class: This is explained in the String section.
- Using string literals.
Java String literals example:
public class Simple { public static void main(String[] args) { String st1 = "John"; String st2 = "Doe"; String st3 = "Jack"; } }
Here the “John”, “Doe” and “Jack” are representing string literals.
When we use string literal, behind the scene an object of type String will be created and the value of the string literal will be stored in that object. This object is stored in a region of the heap memory, which is called string pool.
In the next section, we’ve explained in full details on what string pool is and why we need it. For now, just remember that whenever you’re using sting literal; the compiler behind the scene will create an object of type String via a constructor of this class and put your string literal as the argument to that constructor. In the example above we had:
String st1 = "John";
String st2 = "Doe";
String st3 = "Jack";
For the first variable (the st1): the compile will first create an object of type String like this:
new String("John");
Next, it will store this object in the string pool and finally it will return a reference to that object and stores this reference in the st1 variable. So basically the st1 only has the memory address of the string literal and not the value itself.
The same process happens for the other two variables as well.
So for the second variable st2: the compiler again will create an object of type String and puts the value of the string literal as the argument to the constructor and store the object in the string pool region of the memory. Finally, the st2 will have the memory address of the second object.
Note: the same procedure will happen for the st3 variable as well.
We use the string pool in the Java is for efficiency and performance. For example, if we have 2 or more string literals and all with the same values, the compiler will not create a separate object for each of these values.
Java String literals second example:
String st1 = "John";
String st2 = "John";
Here the compiler sees the first string literals which is “John” and it checks the string pool to see if there’s an object that holds the value “John”. If it found such an object, it will only return a reference of that object and stores it to the st1 variable. Otherwise, if there’s no object with such value, only in that case it will create one.
For the st2 variable, we’re trying to store the same string literal “John”. Here, because an object in the string pool with this value already exists, the compiler won’t create a new one and just returns the reference to that object and stores it to the st2 variable.
Basically, both st1 and st2 are pointing to the same object in the string pool.
As you can see, because of string pool, the compiler didn’t waste resources to create a separate object for each of these string literals. This is an example of efficiency.
Again, in the next section, we’ve explained in more details on how the string pool works.