|
|
Table of Contents |
Thejava.langpackage contains two string classes:StringandStringBuffer. You've already seen theStringclass on several occasions in this tutorial. You use theStringclass when you are working with strings that cannot change.StringBuffer, on the other hand, is used when you want to manipulate the contents of the string on the fly.The
reverseItmethod in the following code uses both theStringandStringBufferclasses 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 (a list of words sorted by ending syllables). Just reverse all the strings in the list, sort the list, and reverse the strings again. You can see thereverseItmethod producing rhyming words in the example in How to Use Pipe Streamsthat shows you how to use piped streams.
Thepublic 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(); } }reverseItmethod accepts an argument of typeStringcalledsourcethat contains the string data to be reversed. The method creates aStringBuffer,dest, the same size assource. It then loops backwards over all the characters insourceand appends them todest, thereby reversing the string. Finally, the method convertsdest, aStringBuffer, to aString.In addition to highlighting the differences between
Strings andStringBuffers, this lesson illustrates several features of theStringandStringBufferclasses: creatingStrings andStringBuffers, using accessor methods to get information about aStringorStringBuffer, modifying aStringBuffer, and converting one type of string to another.
Note to C and C++ Programmers: Java strings are first-class objects, unlike C and C++ strings, which are simply null-terminated arrays of 8-bit characters.
Why Two String Classes?
The Java development environment provides two classes that store and manipulate character data:String, for constant strings, andStringBuffer, for strings that can change.Creating
StringsandStringBuffersThe following statement taken from thereverseItmethod creates a newStringBufferin three steps: declaration, instantiation, and initialization.These are the same steps for creating an object of any type.StringBuffer dest = new StringBuffer(len);Accessor Methods
ThereverseItmethod uses two accessor methods to obtain information about thesourcestring:charAtandlength. BothStringandStringBufferprovide a number of other accessor methods, including some for inspecting substrings and getting the positions of a specific character.Modifying
StringBuffersThereverseItmethod usesStringBuffer'sappendmethod to add characters todest. In addition toappend,StringBufferprovides methods to insert characters into the buffer, modify a character at a specific location within the buffer, and so on.Converting Objects to Strings
reverseItconverts the resultingStringBufferto aStringand returns the string. You can convert several different data types toStrings usingString'svalueOfmethod.Converting
Strings to NumbersYou can also use methods from theInteger,Float,Double, andLongclasses to convert the contents of aStringto a number.Strings and the Java Compiler
The Java compiler usesStrings andStringBuffers behind the scenes to handle literal strings and concatenation.Other Interesting Features
StringandStringBufferprovide several other useful ways to manipulate string data, including concatenation, comparison, substitution, and conversion to upper and lower case.java.lang.Stringand
java.lang.StringBuffersummarize and list all of the methods and variables supported by these two classes.
|
|
Table of Contents |