The string in Java is a reference data type,
as are all other non-primitive data types. This means that the variable contains an address
to an object in memory and not the object itself. The String object is created in the
memory, and the address to it is returned to the variable. The normal syntax to initiate
a reference data type is using the new keyword, which we can also use for strings. To concatenate strings we have the plus sign,
which here is called the concatenation operator (+). It also has an accompanying assignment
operator (+=), which appends one string to another, and creates a new string. Note that while we can break a statement across
multiple lines, we can’t break a String. Instead we have to split them using the concatenation
operator. If we want to add newlines into the string itself we use the escape character
‘n’. This backslash notation is used to write special characters, such as backslash (\)
itself or double-quote (“). They also include the Unicode character notation (u0000) for
writing any character.
is using the equal method of the string class. If we were to use the equality operator instead
it would only compare the memory addresses. Keep in mind that all strings in Java are
objects, so we can call methods directly on constant strings just as well as on variables. The String class has a large number of functions
available, but it contains no methods to manipulate strings. This is because strings in Java are
immutable. Once a string has been created the contents cannot be changed, unless the
whole string is completely replaced. Since most strings are never modified this was done
on purpose to make the String class more efficient. For cases when we need a modifiable string
we instead use the StringBuffer class, which is a mutable String object.
This class has several methods to manipulate strings such as append, delete, and insert.
To convert it back into a regular string there’s the toString method.