java.lang.String

I have not seen a serious Java program that does not use the java.lang.String class. It is one of the most often used classes and definitely one of the most important.

A String object represents a string, i.e. a piece of text. You can also think of a String as a sequence of Unicode characters. A String object can consists of any number of characters. A String that has zero character is called an empty String. String objects are constant. Once they are created, their values cannot be changed. Because of this, String instances are said to be immutable. And, because they are immutable, they can be safely shared.

You could construct a String object using the new keyword, but this is not a common way to create a String. Most often, you assign a string literal to a String reference variable. Here is an example:

String s = "Java is cool";

This produces a String object containing “Java is cool” and assigns a reference to it to s. It is the same as the following.

String message = new String("Java is cool");

However, assigning a string literal to a reference variable works differently from using the new keyword. If you use the new keyword, the JVM will always create a new instance of String. With a string literal, you get an identical String object, but the object is not always new. It may come from a pool if the string “Java is cool” has been created before.

Thus, using a string literal is better because the JVM can save some CPU cycles spent on constructing a new instance. Because of this, you seldom use the new keyword when creating a String object. The String class’s constructors can be used if you have specific needs, such as converting a character array into a String.

Comparing Two Strings

String comparison is one of the most useful operations in Java programming. Consider the following code.

String s1 = "Java";
String s2 = "Java";
if (s1 == s2) {
    ...
}

Here, (s1 == s2) evaluates to true because s1 and s2 reference the same instance. On the other hand, in the following code (s1 == s2) evaluates to false because s1 and s2 reference different instances:

String s1 = new String("Java");
String s2 = new String("Java");
if (s1 == s2) {
    ...
}

This shows the difference between creating String objects by writing a string literal and by using the new keyword.

Comparing two String objects using the == operator is of little use because you are comparing the addresses referenced by two variables. Most of the time, when comparing two String objects, you want to know whether the values of the two objects are the same. In this case, you need to use the String class’s equals method.

String s1 = "Java";
if (s1.equals("Java")) // returns true.

And, sometimes you see this style.

if ("Java".equals(s1))

In (s1.equals(“Java”)), the equals method on s1 is called. If s1 is null, the expression will generate a runtime error. To be safe, you have to make sure that s1 is not null, by first checking if the reference variable is null.

if (s1 != null && s1.equals("Java"))

If s1 is null, the if statement will return false without evaluating the second expression because the AND operator && will not try to evaluate the right hand operand if the left hand operand evaluates to false.

In (“Java”.equals(s1)), the JVM creates or takes from the pool a String object containing “Java” and calls its equals method. No nullity checking is required here because “Java” is obviously not null. If s1 is null, the expression simply returns false. Therefore, these two lines of code have the same effect.

if (s1 != null && s1.equals("Java"))
if ("Java".equals(s1))

String Literals

Because you always work with String objects, it is important to understand the rules for working with string literals.

First of all, a string literal starts and ends with a double quote (“). Second, it is a compile error to change line before the closing double quote. For example, this code snippet will raise a compile error.

String s2 = "This is an important   
        point to note";

You can compose long string literals by using the plus sign to concatenate two string literals.

String s1 = "Java strings " + "are important";
String s2 = "This is an important " +  
        "point to note";

You can concatenate a String with a primitive or another object. For instance, this line of code concatenates a String and an integer.

String s3 = "String number " + 3;

If an object is concatenated with a String, the toString method of the former will be called and the result used in the concatenation.

Escaping Certain Characters

You sometimes need to use special characters in your strings such as carriage return (CR) and linefeed (LF). In other occasions, you may want to have a double quote character in your string. In the case of CR and LF, it is not possible to input these characters because pressing Enter changes lines. A way to include special characters is to escape them, i.e. use the character replacement for them.

Here are some escape sequences:

       \u          /* a Unicode character
       \b          /* \u0008: backspace BS */
       \t          /* \u0009: horizontal tab HT */
       \n          /* \u000a: linefeed LF */
       \f          /* \u000c: form feed FF */
       \r          /* \u000d: carriage return CR */
       \"          /* \u0022: double quote " */
       \'          /* \u0027: single quote ' */
       \\          /* \u005c: backslash \ */

For example, the following code includes the Unicode character 0122 at the end of the string.

String s = "Please type this character \u0122";

To obtain a String object whose value is John “The Great” Monroe, you escape the double quotes:

String s = "John \"The Great\" Monroe";

Switching on A String

Starting from Java 7 you can use the switch statement with a String.

switch(expression) {
case value_1 : 
    statement(s); 
    break; 
case value_2 : 
    statement(s); 
    break;
  .
  .
  .
case value_n : 
    statement(s); 
    break;
default: 
    statement(s);
}

Here is an example of using the switch statement on a String.

String input = ...;
switch (input) {
case "one" : 
    System.out.println("You entered 1.");
    break; 
case "two" : 
    System.out.println("You entered 2.");
    break;
default: 
    System.out.println("Invalid value.");
}

The String Class’s Constructors

The String class provides a number of constructors. These constructors allow you to create an empty string, a copy of another string, and a String from an array of chars or bytes. Use the constructors with caution as they always create a new instance of String.

public String()

Creates an empty string.

public String(String original)

Creates a copy of the original string.

public String(char[] value)

Creates a String object from an array of chars.

public String(byte[] bytes)

Creates a String object by decoding the bytes in the array using the computer’s default encoding.

public String(byte[] bytes, String encoding)

Creates a String object by decoding the bytes in the array using the specified encoding.

The String Class’s Methods

The String class provides methods for manipulating the value of a String. However, since String objects are immutable, the result of the manipulation is always a new String object.

Here are some of the more useful methods.

public char charAt(int index)

Returns the char at the specified index. For example, the following code returns ‘J’.

      "Java is cool".charAt(0)
public String concat(String s)

Concatenates the specified string to the end of this String and return the result. For example, “Java “.concat(“is cool”) returns “Java is cool”.

public boolean equals(String anotherString)

Compares the value of this String and anotherString and returns true if the values match.

public boolean endsWith(String suffix)

Tests if this String ends with the specified suffix.

public int indexOf(String substring)

Returns the index of the first occurrence of the specified substring. If no match is found, returns -1. For instance, the following code returns 8.

     "Java is cool".indexOf("cool")
public int indexOf(String substring, int fromIndex)

Returns the index of the first occurrence of the specified substring starting from the specified index. If no match is found, returns -1.

public int lastIndexOf(String substring)

Returns the index of the last occurrence of the specified substring. If no match is found, returns -1.

public int lastIndexOf(String substring, int fromIndex)

Returns the index of the last occurrence of the specified substring starting from the specified index. If no match is found, returns -1. For example, the following expression returns 3.

      "Java is cool".lastIndexOf("a")
public String substring(int beginIndex)

Returns a substring of the current string starting from the specified index. For instance, “Java is cool”.substring(8) returns “cool”.

public String substring(int beginIndex, int endIndex)

Returns a substring of the current string starting from beginIndex to endIndex. For example, the following code returns “is”:

      "Java is cool".substring(5, 7)
public String replace(char oldChar, char newChar)

Replaces every occurrence of oldChar with newChar in the current string and returns the new String. “dingdong”.replace(‘d’, ‘k’) returns “kingkong”.

public int length()

Returns the number of characters in this String. For example, “Java is cool”.length() returns 12. Prior to Java 6, this method was often used to test if a String was empty. However, the isEmpty method is preferred because it’s more descriptive.

public boolean isEmpty()

Returns true is the string is empty (contains no characters).

public String[] split(String regEx)

Splits this String around matches of the specified regular expression. For example, “Java is cool”.split(” “) returns an array of three Strings. The first array element is “Java”, the second “is”, and the third “cool”.

public boolean startsWith(String prefix)

Tests if the current string starts with the specified prefix.

public char[] toCharArray()

Converts this string to an array of chars.

public String toLowerCase()

Converts all the characters in the current string to lower case. For instance, “Java is cool”.toLowerCase() returns “java is cool”.

public String toUpperCase()

Converts all the characters in the current string to upper case. For instance, “Java is cool”.toUpperCase() returns “JAVA IS COOL”.

public String trim()

Trims the trailing and leading white spaces and returns a new string. For example, ” Java “.trim() returns “Java”.

In addition, there are static methods such as valueOf and format. The valueOf method converts a primitive, a char array, or an instance of Object into a string representation and there are nine overloads of this method.

public static String valueOf(boolean value)
public static String valueOf(char value)
public static String valueOf(char[] value)
public static String valueOf(char[] value, int offset, int length)
public static String valueOf(double value)
public static String valueOf(float value)
public static String valueOf(int value)
public static String valueOf(long value)
public static String valueOf(Object value)

For example, the following code returns the string “23”

String.valueOf(23);

The format method allows you to pass an arbitrary number of parameters. Here is its signature.

public static String format(String format, Object... args)

This method returns a String formatted using the specified format string and arguments. The format pattern must follow the rules specified in the java.util.Formatter class and you can read them in the JavaDoc for the Formatter class. A brief description of these rules are as follows.

To specify an argument, use the notation %s, which denotes the next argument in the array. For example, the following is a method call to the printf method.

String firstName = "John";
String lastName = "Adams";
System.out.format("First name: %s. Last name: %s",
        firstName, lastName); 

This prints the following string to the console:

First name: John. Last name: Adams

Without varargs, you have to do it in a more cumbersome way.

String firstName = "John";
String lastName = "Adams";
System.out.println("First name: " + firstName + 
        ". Last name: " + lastName);

Note

The printf method in java.io.PrintStream is an alias for format.

The formatting example described here is only the tip of the iceberg. The formatting feature is much more powerful than that and you are encouraged to explore it by reading the Javadoc for the Formatter class.