public final class String
implements Serializable, Comparable, CharSequence
In java the String class represents character strings. All string literals in Java programs, such as “abc”, are implemented as instances of this class.
As with any other object in java, you can create String objects by using the new keyword and a constructor. The String class has 13 constructors that allow you to provide the initial value to string. String represented in double quotes like “abcd”
are referred as String literal which are created in string pool.
Creating a String literal using double quotes, JVM looks in the String pool to find if any other String is stored with same value. If found, it just returns the reference to that String object else it creates a new String object with given value and stores it in the String pool.
When we use new operator, JVM creates the String object but don’t store it into the String Pool. We can use intern() method to store the String object into String pool or return the reference if there is already a String with equal value present in the pool.
String is immutable and final in Java; their values cannot be changed after they are created and every modification in String result creates a new String object
Immutability offers lot of benefit to the String class. Being immutable String in Java caches its hashcode, and do not calculate every time we call hashcode method of String, which makes it very fast as hashmap key to be used in hashmap in Java.
As strings are immutable, then it need to be final as well, so that subclass doesn’t break immutability. Because String is final it can be safely shared between multiple threads without any extra synchronization(better thread safety).
Reason for a string to be immutable would be string pool, as objects in string pool may be refered by different location, if string is not immutable changing value of one object would effect other as well.In short string pool is possible because string in java are immutable
Next reason would be security, Strings are used in java to pass url etc being immutable make it more secured, otherwise it would be possible for hacker change url and potentially gsin acess to location which they are not authorised.
When you compare two String literals using equality operator “==” or equal method, it returns true because they are actually same instance of String class.
The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.
A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String.
The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).