What is a Java String? In Java, a string is an object that represents a sequence of characters or char values. The java.lang.String class is used to create a Java string object.
There are two ways to create a String object:
Now, let us understand the concept of Java String pool.
Java String Pool: Java String pool refers to collection of Strings which are stored in heap memory. In this, whenever a new object is created, String pool first checks whether the object is already present in the pool or not. If it is present, then same reference is returned to the variable else new object will be created in the String pool and the respective reference will be returned. Refer to the diagrammatic representation for better understanding: