| 1. |
Explain String pool in Java. |
|
Answer» String Pool, also known as SCP (String Constant Pool), is a special storage space in Java heap memory that is used to store unique string objects. Whenever a string object is created, it FIRST CHECKS whether the String object with the same string value is already present in the String pool or not, and if it is available, then the reference to the string object from the string pool is returned. Otherwise, the new string object is ADDED to the string pool, and the respective reference will be returned. As SHOWN in the above image, two Strings s1 and s2 are created with the values "Apple" and "Mango". Therefore, when the third String S3 containing the value "Apple" is created, instead of creating a new object, the existing object reference will be returned. Here, s1==s2 is false both strings s1 and s2 refer to different string values from the string pool i.e. apple and mango. We can see that s1==s3 is true because both strings s1 and s3 refer to a single string value from a string pool i.e., apple. |
|