Welcome to "Coding Center"!! Coding Center is the place where you will find something new to learn about Java and Python. If you are looking to brush up on your skills before going for an interview, this article is for you...

String Pool | Another Interview Question

In this article, we are going to cover one of most favorite, core, and frequently asked interview questions - What is String Pool and why do we need it? What are the benefits of using String Pool? Let's answer these questions one by one. Shall we?

What is String Pool?

String Pool is a memory space provided to us by JVM as a thought that if we keep using String Pool, we will save the headache of more frequent Garbage Collection and the performance will be better. Now, how does this actually happen? Let's understand this better.

When String was created, everyone knew that it will be the most used class and as Java works on objects we will be creating a lot of String objects which will be harder to store and garbage collected, and if the size of these objects is not maintained properly then it will reduce the performance of the application. Thinking of all this, String Pool came into existence.

String Pool is a special memory space where Strings are stored by JVM. Now, there is another big term here 'Interning' which means that one and only one copy of each String Literal will be stored in String pool. Now, how does it benefits us? Let's take an example now.

String s1 = "Hello";
String s2 = "Hello";
		
String s3 = "Namaste";
		
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // false
		
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false

In this example, we can see that we have created two String Literals - s1 and s2. Though both of them are different variables they will point to the same object in String Pool. Now, why's that? As I said in previously, one and only copy of each String Literal will be stored. Through JVM's eyes, it will look like this - 


Now, String Literal is not the only way of creating Strings, we can also use new keyword to create Strings like this  -

String s1 = new String("Hello");

But, it is always recommended to create more String Literals and fewer String Objects if you want better performance. Now, you might have guessed why's that. But, let me tell you if we use more String Literals then whenever JVM sees that there is an already existing object with the same data then it will not create another one which will help in making performance better.

Now, we can also convert a String Object to String Literal using intern method as shown below - 

String s1 = "Hello";
String s2 = "Hello";

String s3 = new String("Hello");

System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
		
s3 = s3.intern(); // Convert String Object to String Literal
		
System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // true

What can be asked more in the Interview?

An Interviewer can also ask a question on the basics that we have covered above. He/She can ask a question on this like given two Strings, how many objects will be created in Heap memory. See the below example- 

String s1 = "Hello";
String s2 = "Hello";

String s3 = new String("Hello");
String s4 = new String("Hello");
		
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
System.out.println(s3.equals(s4)); // true

System.out.println(s1 == s2); // true
System.out.println(s1 == s3); // false
System.out.println(s3 == s4); // false

Here, as we know, only one object of the same data will be created for String Literal but not for String Objects. So, if we create multiple String Objects using new keyword with the same data, then in Heap it will create multiple objects.

3 comments:

| Designed by Colorlib