String Constant Pool In Java with Example

In this post, we will see String Constant Pool In Java with Example. We will see what is String constant pool, how memory allocation happens for the string object. Also, we will see what happens when we create a String object without a new operator and with a new operator.

What is String Constant Pool.

String uses a special memory location to reuse of String objects, is called String Constant Pool. Suppose we create a String object without new keyword i.e String s1 = “ram” and again we do String s2 = “ram” and String s3 = “ram”. Here String will create only one object with value ram. We will have three reference s1, s2, and s3 which are pointing the same object “ram”.  Let’s see in below diagram.

String Constant Pool In Java

Some points for String Constant Pool In Java with Example.

Let’s see how object is getting created in case of String.

  1. When first time we do String s1 = “ram”, JVM will create a new object in String Constant Pool and s1 will refer to that object i.e “ram”.
  2. When we do the second time  String s2 = “ram”, JVM will check is there any object with value “ram”  already there in String Constant pool. As of now yes we have already “ram” is there in String Constant pool, so it will not create a new object, just s2 reference variable will point to that existing “ram” object. The same process will happen for String s3 = “ram”.

Creating String object with new operator – Let’s see another scenario, In this case, we are going to create a String Object with the new keyword.

String str1  = new String(“mohan”);

Here the first JVM will check, is there any object available with the name “mohan”  in String Constant Pool or not, if not it will create one object inside String Constant Pool and it will also create another object outside of String Constant Pool and str1 will point this object(the object which has been created outside of String Constant Pool). Let’s see in below diagram.

String Constant Pool In Java

Let’s see another scenario example.

String st1 = “rakesh”;

String st2 = new String(“rakesh”);

How memory allocation will happen in the above case. How many objects will be created?

When we do String st1 = “rakesh”, JVM will create one object in String Constant pool and st1 will refer it, something as in below diagram.

String Constant Pool In Java

Now again we are doing String st2 = new String(“rakesh”);

JVM will check is any object available with the name “rakesh” in String Constant Pool or not, for now yes we have already “rakesh” in String Constant Pool, so JVM will not create any object inside String Constant Pool. It will create one object outside of String Constant Pool, and st2 will point to that object. Finally, memory location will look like as below.

Point to remember – When we create an object of String with the new operator, JVM will check first in SCP, the object is available or not. If that object is not available inside SCP JVM will create two objects, one inside SCP and another one outside of SCP. But if JVM finds the same object inside SCP, it will only create one object outside of SCP.

So now the question arises in the below case how many objects will be created?

String st1 = “rakesh”;

String st2 = new String(“rakesh”);

Answer is only two object will be created.

Again suppose we do  String st2 = new String(“shyam”); Here also two object will be created. One inside SCP and another one is outside of SCP/Inside heap. Why ? because JVM will look for “shyam” in SCP, it will not find it and will create one object inside SCP and another one inside heap.

Some points for String Constant Pool In Java with Example.

Now we have a basic idea, what is String Constant Pool, how memory allocates in case of String. Let’s see some more scenario which you might encounter during inetrview.

How many objects will be created in below code snippet?

public class StringExample1 {
	public static void main(String[] args) {

		String s1 = "india";
		String s2 = s1 + "is";
		s1.concat("great");
		s2.concat(s1);
		s1 += "country";
		System.out.println(s1 + "   " + s2);

	}
}

Output is – indiacountry indiais

Total eight String objects will be created. Let’s see how –

String Constant Pool In Java

First “india” will be created in String Constant Pool and s1 will refer it, then “is” will be created in SCP. Now “indiais” is created inside heap and s2 will refer it. Till now s1 contains “india” and s2 contains “indiais”, now “great” will be craeted in SCP and “indiagreat” will be created in heap, but point to notice here nothing is going refer these objects, these objects will be lost. Come to s2.concat(s1), it will create new object “indiaisindia”. Why? don’t forget s2 contains “indiais” and we are doing concat(), but again it will be lost. now “country” will get created in SCP and “indiacountry” will be created in heap, now s1 will point “indiacountry”. Som

What is the output of the below program?

public class StringExample1 {
	public static void main(String[] args) {
		
		String s1 = "ram";
		String s2 = "ram";
		
		String s3 = new String("ram");
		String s4 = new String("ram");
		
		StringBuffer s5 = new StringBuffer("ram");
		StringBuffer s6 = new StringBuffer("ram");
		
		System.out.println("comparing s1 and s2 with == " +(s1==s2));
		
		System.out.println("comparing s1 and s2 with equals()  "+s1.equals(s2));
		
		System.out.println("comparing s1 and s3 with == " +(s1==s3));
		
		System.out.println("comparing s1 and s3 with equals() " +s1.equals(s3));
		
		System.out.println("comparing s3 and s4 with == " +(s3==s4));
		
		System.out.println("comparing s3 and s4 with equals() " +s3.equals(s4));
		
		System.out.println("comparing s5 and s6 with == " +(s5==s6));
		
		System.out.println("comparing s5 and s6 with equals() " +s5.equals(s6));
		
		
	}
}

Outout is –

comparing s1 and s2 with == true
comparing s1 and s2 with equals() true
comparing s1 and s3 with == false
comparing s1 and s3 with equals() true
comparing s3 and s4 with == false
comparing s3 and s4 with equals() true
comparing s5 and s6 with == false
comparing s5 and s6 with equals() false

Comparison of two String using == and equals(). Some points for String Constant Pool In Jaa with Example.

Before proceeding let’s see  differences between == and equals(). == operator checks for references, if two different reference variable is pointing the same object == will return true, while equals() method is overridden in String class and it will check for content.

Example 1 – Creating a String object without using the new keyword.

public class StringExample1 {
	public static void main(String[] args) {

		String s1 = "ram";
		String s2 = "ram";

		System.out.println(s1 == s2);
		System.out.println(s1.equals(s2));

	}
}

Output is –

true
true

  • In the case of s1 == s2, both reference variables s1 and s2 pointing to the same object “ram” and == checks for reference, the output will be true.
  • In the case of s1.equals(s2), it will check for content, which is also the same. Output is true

Example 2 – We have two String objects which have been created using the new keyword.

public class StringExample2 {
	public static void main(String[] args) {

		String st1 = new String("rakesh");
		String st2 = new String("rakesh");

		System.out.println(st1 == st2);
		System.out.println(st1.equals(st2));

	}
}

Output –

false
true

  • In the case of st1 == st2, both reference variables st1 and st2 pointing to the different object “rakesh” and == checks for reference, the output will be false.
  • In the case of st1.equals(st2), it will check for content, which is the same. The output is true.

Some points for String Constant Pool In Java with Example.

Example 3 – We have two String objects, one created using without a new keyword and another one created with the new keyword.

public class StringExample3 {
	public static void main(String[] args) {

		String st1 = "rakesh";
		String st2 = new String("rakesh");

		System.out.println(st1 == st2);
		System.out.println(st1.equals(st2));

	}
}

  • In the case of st1 == st2,  st1 is pointing to the object “rakesh” which is inside String Constant Pool and st2 is pointing to “rakesh” which is in heap. Agin == checks for reference, the output will be false.
  • In the case of st1.equals(st2), it will check for content, which is the same. The output is true.

Example 4 – We have two StringBuffer objects. In the case of StringBuffer, we can create an object with the new keyword.

public class StringExample4 {
	public static void main(String[] args) {

		StringBuffer sb1 = new StringBuffer("ram");
		StringBuffer sb2 = new StringBuffer("ram");

		System.out.println(sb1 == sb2);
		System.out.println(sb1.equals(sb2));
	}
}

Output is –

false
false

  • In the case of StringBuffer, there are two different objects will be created inside the heap.  So sb1==sb2 will return false. Now the question is why sb1.equals(sb2) returning false, even both have the same content. Always remember String Overrides equals() and hashCode() method, and it has been implemented some logic inside equals() and hashCode(). But StringBuffer doesn’t override equals() and hashCode(), because if this reason even if the content is the same StringBuffer returns false.

That’s all about String Constant Pool in Java with example.

You may like

String docs

Summary –  We have seen about String Constant Pool In Java in details. We covered how memory allocation happens for String objects. Also covered what is the difference between == and equals() method.