News & UpdatesProgrammingWeb programming StoreMy Projects

Java Tutorial – 06 – String

The String class in Java is a data type that can hold string literals. String is a reference data type, as are all non-primitive data types. This means that the variable contains an address to an object in the memory, and not the object itself. A String object is created in the memory, and the address to the object is returned to the variable.

As seen below, string literals are delimited by double quotes. This is actually a shorthand notation for the regular reference type initialization (creation) syntax, which uses the new keyword.

String a = "Hello";
String b = new String(" World");

Combining strings

The plus sign is used to combine two strings. It is known as the concatenation operator (+) in this context. The operator has an accompanying assignment operator (+=), which appends one string to another and creates a new string.

String c = a+b; // Hello World
       a += b;  // Hello World

Note that while a statement may be divided into multiple lines a string must be on a single row, unless it is split up by using the concatenation operator.

String x
         = "Hello " + 

Escape characters

For adding new lines to the string itself, there is the escape character “\n“. This backslash notation is used to write special characters, such as backslash itself or a double-quote. Among the special characters is also a Unicode character notation for writing any character. All of the escape characters can be seen in the following table.

\nnewline\fform feed
\thorizontal tab\’single quote
\bbackspace\”double quote
\rcarriage return\\backslash
\uFFFFUnicode character
(4-digit hex number)

String compare

The way to compare two strings is by using the equals method of the String class. If the equality operator (==) is used, the memory addresses will be compared instead.

boolean x = a.equals(b); // compares string
boolean y = (a == b);    // compares address

Bear in mind that all strings in Java are String objects. Therefore, it is possible to call methods directly on constant strings, just as on variables.

boolean z = "Hello".equals(a);

StringBuffer class

The String class has a large number of methods available, but it does not contain any methods for manipulating strings. This is because strings in Java are immutable. Once a String object 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 you need a modifiable string you can use the StringBuffer class, which is a mutable string object.

StringBuffer sb = new StringBuffer("Hello");

This class has several methods to manipulate strings, such as append, delete and insert.

sb.append(" World");   // add to end of string
sb.delete(0, 5);       // remove 5 first characters
sb.insert(0, "Hello"); // insert string at beginning

A StringBuffer object can be converted back into a regular string with the toString method.

String s = sb.toString();
Recommended additional reading:
Sams - Teach Yourself Java in 24 Hours