![]() |
VOOZH | about |
A String in Java is an object used to store a sequence of characters enclosed in double quotes. It uses UTF-16 encoding and provides methods for handling text data.
Example:
String name = "Geeks";
String num = "1234";
Geeks
There are two ways to create a string in Java:
To make Java more memory efficient (because no new objects are created if it exists already in the string constant pool).
Example:
String str = “GeeksforGeeks”;
Using the new keyword creates a new object in heap memory, even if the same string already exists in the pool.
Example:
String str = new String (“GeeksforGeeks”);
The CharSequence interface represents a sequence of characters in Java. It provides common methods such as length(), charAt(), subSequence(), and toString() for working with character data.
In Java, string objects are immutable. Immutable simply means unmodifiable or unchangeable. Once a string object is created its data or state can't be changed but a new string object is created.
Sachin
Explanation: In the above example, the String.concat() does not modify the original String object. When str.concat(" World") is executed:
Whenever a String Object is created as a literal, the object will be created in the String constant pool. This allows JVM to optimize the initialization of String literal. The string constant pool is present in the heap.
Example 1: Using String literals to assigning char sequence value.
String str1 = "Hello";
Example 2: When we initialize the same char sequence using string literals.
String str1 = "Hello";
String str2 = "Hello";
Strings can also be created using the new keyword, which allocates a new object in heap memory. However, the string literal inside it is still stored in the String Constant Pool (if not already present).
Example 1: Using new keyword to assign a char sequence to a String object.
String str1 = new String("John"); String str2 = new String("Deo");
The intern() method returns a reference from the string constant pool. If the string is not already present in the pool, it is added. Otherwise, the existing reference from the pool is returned.
Example 2: Using .intern() to add a string object in string constant pool.
// this will add the string to string constant pool.
String internedString = demoString.intern();
It is preferred to use String literals as it allows JVM to optimize memory allocation.
If we notice if we use new keyword or string literals both store the values in the string but the difference is if we use the string literals or intern() the string object it will store the values in the string constant pool which is present inside the heap as shown in the image.
Before Java 7, the String Constant Pool was stored in PermGen (Permanent Generation) memory, which had limited space. From Java 7 onward, the String Pool was moved to the Heap Memory to improve memory management and scalability.
For example:
String demoString = new String("Bhubaneswar");
Let us have a look at the concept with a Java program and visualize the actual JVM memory structure:
Below is the implementation of the above approach:
TAT TAT TAT TAT
Note: All objects in Java are stored in a heap. The reference variable is to the object stored in the stack area or they can be contained in other objects which puts them in the heap area also.