The String class is one of the most widely used class in Java. String represents character sequence. For example:- "Java" is a string which has five characters. All the string literals are instance of String class.
There are two ways to create String object:-
By string literal
String str1 = "Modern Pathshala"; String str3 = "Modern Pathshala";
When we create a string object using string literal, that string literal is added to the string pool, if it is not present there already and later if we create another object with same string using literal, then a reference of the string literal already present in string pool is returned as shown in below image -
By new keyword
String str2 = new String("Modern Pathshala");
When we create any string using new operator then the object is created in heap memory. The above diagram represents above strings created in memory.
java.lang.Stringclass is final which implies no class and extend it.
In this chapter, we learnt about String class and how to create String in Java. In our next chapter we will understand various
methods of String class and their usage in programming.