Java String
Definition
A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you will acquire a knowledge of the String class and String methods, along with several other tutorials of the Java String .
Introduction
Java strings are objects that are internally supported by char arrays. Since the array is immutable (not extensible), the string is also immutable. Each time a string is changed, a whole updated string is genrated.
In Java, string is basically an object that depicts a sequence of character values. Arrays of characters work just like Java strings. For example:
char[] ch={'t','u','t','o','r','i','a','l','a','n','d','e','x','a','m','p','l','e'};
String name =new String(ch);
It is same as:
String name ="tutorialandexample";
Memory allotment of String
Whenever a String object is created as a literal, it is created in the String constant pool. This allows the JVM to streamline the initialization of string literals.
Example:
String str = "Hello";
The string can also be declared using the new operator. That is, it can be dynamically assigned. When the string is dynamically allocated, new space is allocated in the heap that means it is not included to the string constant pool.
The java.lang.String class implements the Serializable, Comparable, and CharSequence interfaces.

CharSequence Interface
We already know that the CharSequence interface is utilized to represent a string. The String, StringBuffer, and StringBuilder are the classes that implements it. This means that you can utilize the following three classes with the help of which you can create strings in Java:

Java strings are immutable. That is, it cannot be changed. Every time the string gets changed, a new instance is created. For variable strings, you can use the StringBuffer and StringBuilder classes.
I'll talk about immutable strings later. First, let's understand what a String is in Java and how a String object is created.
Define String in Java
In general, a string is a series of chars. However, in Java, String is an object that depicts a series of characters. The java.lang.String class is utilized to generate a string object.
Creating a String object
Following are the two ways with the help of which we can create String object:
- By string literal
- By new keyword
String Literal
We can build Java String literal with the help of double quotes.
For Example:
String name ="TutorialandExample";
Whenever you create a string literal, the JVM first checks for a "string constant pool". In case, the string exists in the pool previously, an instance to the pooled instance is returned. In case, the string does not exist in the pool, a new string instance is build and put down in the pool.
For example:
String name1 ="TutorialandExample";
String name2 ="TutorialandExample"; //won’t create a new instance
In the above example, single object will be created. First, the JVM does not find a string object with the value "TutorialandExample" in the string constant pool, so it builds a new object. It then searches the pool for a string containing the value "TutorialandExample" and does not create a new object, but returns an instance to the same.
Use Of String literal in Java
To increase Java memory efficiency (because no new object is created if it already exists in the string constant pool).
Disadvantages
As mentioned at the beginning, String is a Java object. However, I didn't build a String object with the help of new keyword mentioned above. The compiler does this and uses a String literal (which in this case provides "Welcome") to create a String object and assign it to the provided String instance.
However, if the object already exists in memory, no new object will be created, but the same old object will be assigned to the new instance. That is, a string object that has the above two string instances (str1 and str2), but was created by the compiler (with the value "TutorialandExample") and both instances were assigned the same thing. Considering the above example, there are 18 string instances with the same value. This means that there is only one object with a value in memory and all 18 string instances point to the same object.
By new keyword
String name=new String("TutorialandExample");
In such cases, the JVM creates a new String object in the regular (non-pooled) heap and the literal "TutorialandExample" is put down in the string constant pool. The variable name refers to an object in the heap (non-pool).