![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
The Java platform provides three classesString
,
StringBuffer
and
StringBuilder
which store and manipulate strings character data consisting of more than one character. Use the following guidelines for deciding which class to use:
- If your text is not going to change, use a string a
String
object is immutable.- If your text will change, and will only be accessed from a single thread, use a string builder.
- If your text will change, and will be accessed from multiple threads, use a string buffer.
Following is a sample program called
StringsDemo
, which reverses the characters of a string. This program uses both a string and a string builder.
The output from this program is:public class StringsDemo { public static void main(String[] args) { String palindrome = "Dot saw I was Tod"; int len = palindrome.length(); StringBuilder dest = new StringBuilder(len); for (int i = (len - 1); i >= 0; i--) { dest.append(palindrome.charAt(i)); } System.out.format("%s%n", dest.toString()); } }The following sections discuss several features of thedoT saw I was toDString
,StringBuffer
, andStringBuilder
classes.
![]() ![]() ![]() |
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.