The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Learning the Java Language
Lesson: Object Basics and Simple Data Objects

Creating Strings and String Buffers

A string is often created from a string literal—a series of characters enclosed in double quotes. For example, when it encounters the following string literal, the Java platform creates a String object whose value is Gobbledygook.
"Gobbledygook"
The StringsDemo program uses this technique to create the string referred to by the palindrome variable:
String palindrome = "Dot saw I was Tod";
You can also create String objects as you would any other Java object: using the new keyword and a constructor. The String class provides several constructors that allow you to provide the initial value of the string, using different sources, such as an array of characters, an array of bytes, a string buffer, or a string builder. The following table shows the constructors provided by the String class.

*Constructors in the String Class
Constructor Description
String() Creates an empty string.
String(byte[])
String(byte[], int, int)
String(byte[], int, int, String)
String(byte[], String)
Creates a string whose value is set from the contents of an array of bytes. The two integer arguments, when present, set the offset and the length, respectively, of the subarray from which to take the initial values. The String argument, when present, specifies the character encoding to use to convert bytes to characters.
String(char[])
String(char[], int, int)
Creates a string whose value is set from the contents of an array of characters. The two integer arguments, when present, set the offset and the length, respectively, of the subarray from which to take the initial values.
String(String) Creates a string whose value is set from another string. Using this constructor with a literal string argument is not recommended, because it creates two identical strings.
String(StringBuffer) Creates a string whose value is set from a string buffer.
String(StringBuilder) Creates a string whose value is set from a string builder.
* The String class defines other constructors not listed in this table. Those constructors have been deprecated, and their use is not recommended.

Here's an example of creating a string from a character array:

char[] helloArray = { 'h', 'e', 'l', 'l', 'o' };
String helloString = new String(helloArray);
System.out.println(helloString);
The last line of this code snippet displays: hello.

You must always use new to create a string buffer or string builder. Because the two classes have similar constructors, the following table lists only the constructors for string buffers.

Constructors in the StringBuffer Class
Constructor Description
StringBuffer() Creates an empty string buffer whose initial capacity is 16 characters.
StringBuffer(CharSequence) Constructs a string buffer containing the same characters as the specified CharSequence. This constructor was introduced in JDK 5.0.
StringBuffer(int) Creates an empty string buffer with the specified initial capacity.
StringBuffer(String) Creates a string buffer whose value is initialized by the specified String. The capacity of the string buffer is the length of the original string plus 16.

The StringsDemo program creates the string builder referred to by dest, using the constructor that sets the buffer's capacity:

String palindrome = "Dot saw I was Tod";
int len = palindrome.length();
StringBuilder dest = new StringBuilder(len);
This code creates the string builder with an initial capacity equal to the length of the string referred to by the name palindrome. This ensures only one memory allocation for dest because it's just big enough to contain the characters that will be copied to it. By initializing a string builder or string buffer's capacity to a reasonable first guess, you minimize the number of times memory must be allocated for it. This makes your code more efficient because memory allocation is a relatively expensive operation.

Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2005 Sun Microsystems, Inc. All rights reserved.