Java Programming/API/java.lang.String

is a class built into the Java language defined in the  package. It represents character strings. Strings are ubiquitous in Java. Study the  class and its methods carefully. It will serve you well to know how to manipulate them skillfully. String literals in Java programs, such as "abc", are implemented as instances of this class like this:

On the right hand side a String object is created represented by the string literal. Its object reference is assigned to the  variable.

Immutability
Strings are immutable; that is, they cannot be modified once created. Whenever it looks as if a String object was modified actually a new String object was created. For instance, the  method returns the string with leading and trailing whitespace removed. Actually, it creates a new trimmed string and then returns it. Pay attention on what happens in Code section 3.82:

The  method call does not modify the object so nothing happens. It creates a new trimmed string and then throws it away.

The returned string is assigned to the variable. It does the job as the  method has created a new   instance.

Concatenation
The Java language provides special support for the string concatenation with operator :

The concatenation is not always processed at the same time. Raw string literals concatenation is done at compile time, hence there is a single string literal in the byte code of the class. Concatenation with at least one object is done at runtime.

operator can concatenate other objects with strings. For instance, integers will be converted to strings before the concatenation:

Each Java object has the  inherited from the  class. This method provides a way to convert objects into s. Most classes override the default behavior to provide more specific (and more useful) data in the returned  :

Using StringBuilder/StringBuffer to concatenate strings
Remember that  objects are immutable objects. Once a  is created, it can not be modified, takes up memory until garbage collected. Be careful of writing a method like this:

On the  operation a new  object is created at each iteration. Suppose  contains the elements. At runtime, the method creates thirteen s: Even though only the last one is actually useful.

To avoid unnecessary memory use like this, use the  class. It provides similar functionality to s, but stores its data in a mutable way. Only one  object is created. Also because object creation is time consuming, using  produces much faster code.

As  isn't thread safe (see the chapter on Concurrency) you can't use it in more than one thread. For a multi-thread environment, use  instead which does the same and is thread safe. However,  is slower so only use it when it is required. Moreover, before Java 5 only  existed.

Comparing Strings
Comparing strings is not as easy as it may first seem. Be aware of what you are doing when comparing 's using  :

The difference between the above and below code is that the above code checks to see if the 's are the same objects in memory which they are. This is as a result of the fact that 's are stored in a place in memory called the String Constant Pool. If the  keyword is not explicitly used when creating the  it checks to see if it already exists in the Pool and uses the existing one. If it does not exist, a new Object is created. This is what allows Strings to be immutable in Java. To test for equality, use the  method inherited by every class and defined by   to return  if and only if the object passed in is a   contains the exact same data:

Remember that the comparison is case sensitive.

To order  objects, use the   method, which can be accessed wherever we use a String datatype. The  method returns a negative, zero, or positive number if the parameter is less than, equal to, or greater than the object on which it is called. Let's take a look at an example:

The code section 3.92 is comparing the String variable  to. If  is different even in the slightest manner, we will get a value above or below 0 depending on the exact difference. The result is negative if this String object lexicographically precedes the argument string. The result is positive if this String object lexicographically follows the argument string. Take a look at the Java API for more details.

Splitting a String
Sometimes it is useful to split a string into separate strings, based on a regular expressions. The class has a   method, since Java 1.4, that will return a String array:

Another useful application could be to split the String text based on the new line character, so you could process the text line by line.

Substrings
It may also be sometimes useful to create substrings, or strings using the order of letters from an existing string. This can be done in two methods.

The first method involves creating a substring out of the characters of a string from a given index to the end:

The index of the first character in a string is 0.

By counting from there, it is apparent that the character in index 3 is the second "f" in "coffee". This is known as the. All characters from the  until the end of the string will be copied into the new substring.

The second method involves a user-defined  and  :

The string returned by  would be "port".

Please note that the endIndex is not inclusive. This means that the last character will be of the index. Therefore, in this example, every character from index 3 to index 6, inclusive, was copied into the substring.

String cases
The  class also allows for the modification of cases. The two methods that make this possible are  and.

These methods are useful to do a search which is not case sensitive:

Question 3.12: You have mail addresses in the following form:

Write the  method that receives the mail string as parameter and returns the readable person name like this:


 * 1) We only process non null strings,
 * 2) We first split the mail into two parts to separate the personal information from the company information and we keep the name data,
 * 3) Then we split the name information to separate the first name from the last name. As the   method use regular expression and   is a wildcard character, we have to escape it . However, in a string, the   is also a special character, so we need to escape it too ,
 * 4) The last name is just capitalized,
 * 5) As the case of all the first name characters will not be the same, we have to cut the first name. Only the first name initial will be capitalized,
 * 6) Now we can concatenate all the fragments. We prefer to use a   to do that.