Previous | Next | Trail Map | Writing Java Programs | Table of Contents


The String and StringBuffer Classes

The reverseIt() method in the following class uses both the String and StringBuffer classes to reverse the characters of a string. If you have a list of words, you can use this method in conjunction with a sort program to create a list of rhyming words (at least a list of words with the same ending syllables). Just reverse all the strings in the list, sort the list, and reverse the strings again.
class ReverseString {
    public static String reverseIt(String source) {
	int i, len = source.length();
	StringBuffer dest = new StringBuffer(len);

	for (i = (len - 1); i >= 0; i--) {
	    dest.append(source.charAt(i));
	}
	return dest.toString();
    }
}
The reverseIt() method accepts an argument of type String called source which contains the string data to be reversed. The method creates a StringBuffer, dest, the same size as source, then loops backwards over all the characters in source and appends them to dest thereby reversing the string. Finally, the method converts dest, a StringBuffer, to a String.

In addition to highlighting the differences between Strings and StringBuffers, this lesson illustrates several features of the String and StringBuffer classes: creating Strings and StringBuffers, using accessor methods to get information about a String or StringBuffer, modifying a StringBuffer, and converting one type of string to another.

Why Two String Classes?

The Java development environment provides two classes that store and manipulate character data: String, for constant strings, and StringBuffer, for mutable strings.

Creating Strings and StringBuffers

This line from the method above
StringBuffer dest = new StringBuffer(len);
creates a new StringBuffer in three steps: declaration, instantiation, and initialization. These are the same steps for creating an object of any type.

Accessor Methods

The reverseIt() method uses two accessor methods to obtain information about source: charAt() and length(). Both String and StringBuffer provide a number of other accessor methods, including some for inspecting substrings and getting the positions of a specific character.

Modifying StringBuffers

The reverseIt() method uses StringBuffer's append() method to add characters to dest. In addition to append(), StringBuffer provides methods to insert characters into the buffer or modify a character at a specific location within the buffer, among others.

Converting Objects to Strings

reverseIt() converts the resulting StringBuffer to a String and returns the string. You can convert several different data types to Strings using String's valueOf() method.

Converting Strings to Numbers

You can also use methods from the Integer, Float, Double, and Long classes to convert the contents of a String to a number.

Other Interesting Features

String and StringBuffer provide several other useful ways to manipulate string data, including concatenation, comparison, substitution, and converting to upper and lower case. java.lang.String and java.lang.StringBuffer summarize and list all of the methods and variables supported by these two classes.

Strings and the Java Compiler

Before moving on to another lesson, you need to understand one final, important peculiarity about Strings and StringBuffers. The Java compiler uses Strings and StringBuffers behind the scenes to handle literal strings and concatenation.

Note to C and C++ Programmers: Java strings are first-class objects; whereas C and C++ strings are simply null-terminated arrays of characters. For more information see Java Strings are First-Class Objects .


Previous | Next | Trail Map | Writing Java Programs | Table of Contents