String Classes

 

 

 

Foreword

Resources

Code Listings

Foil Sets

Assignments

Solutions

External Resources

SiteMap

Search

Home

 

Up ] Thread Base Applet ] Math Class ] [ String Classes ] Off Screen GC ] JavaApplet Context ] Events ] Networking ] Java Appl Design ]

The String Classes

Java provides the programmer with two string classes -- String and StringBuffer. The String class is for strings that are constant -- they will not change. The StringBuffer class is for strings that are not constant and need to be altered. C and C++ do not provide any mechanism for strings at all; they are merely represented by null terminated arrays of characters. Using arrays for strings is wholly unsatisfactory in an environment like Java, where security and portability demand a much more robust approach. The danger of allowing strings to be represented by character arrays is that you always run the risk of overrunning the boundaries of the array. By encapsulating Strings in classes, the programmer does not have to worry about this problem.

Using Strings

The String class holds constant strings that connot be changed once they are initialized. There are several constructors for Strings, depending upon how your String data is organized. The most basic constructor is that used for the string literals. When the Java compiler encounters a String literal, it actually creates an instance of the String class for that literal. Therefore, the easiest way to initialize a String object is to use the String literal

    String s = "Hello, World!";

In additiion, you can initialize the String in the traditional maner, by either including another String object, or a string literal.

    String s = new String(someString);

You can also give the String constructor a character array for initialization. You can either pass the whole array,

    public String(char value[])

or just a portion of the array, or subarray,

    public String(char value[], int offset, int count)

"offset" indicates where the subarray begins, and "count" indicates how long the subarray is.

Make sure that you do not alter the array after you use it to create the String. The array is not copied, and any alterations made to it will change the value of the characters in the String.

Additionally, you can use an array of bytes to initialize a String, in the same manner as an array of characters.

    public String(byte ascii[], int hibyte)

    public String(byte ascii[], int hibyte, int offset, int count)

In the case of an array of bytes, because Java uses the UNICODE character set, you need to indicate what the upper 8 bits of the 16-bit character are. You do this by using the hibyte argument. Typically the hibyte will be zero (0) for the Latin character set.

There are many methods provided for the String class. While this multitude of options can be confusing, there is undoubtedly a way to do something you want with this class. For methods for the String class please see JAVA API reference : java.lang.String

The StringBuffer Class

The StringBuffer class provides methods for implementing a modifiable string. Whereas the String class dealt with strings that weren't meant to change, the StringBuffer class is essentially an expanding buffer for characters and other types to be used in creating Strings. This class provides methods for inserting and appending data to the String. Once you are finished creating your String, you can convert it with toString() to a String object that you can then manipulate with the String class methods.

Remember to use the StringBuffer class if you want to hold strings that you know will be changing.

There are three constructors for the StringBuffer class:

public StringBuffer(). This constructor creates an empty StringBuffer.
public StringBuffer(int length). This constructor creates a StringBuffer the size of length.
public StringBuffer(String str). This constructor creates a StringBuffer with the string str.

The methods implemented by the StringBuffer class are meant to allow you to create Strings from combinations fo incoming types such as characters, integers, floats, and objects. The following example presents a use of the StringBuffer to build up a String from keyboard input:

    StringBuffer str = new StringBuffer();
    System.out.println("Enter Width:");
    while ((ch = System.in.read()) != '\n') {
        str.append(ch);
    }

Notice that the StringBuffer was initialized as empty, and that the class automatically allocated space for new characters at the end. You can check out the online API guide on StringBuffer class : java.lang.StringBuffer

Whenever you use the StringBuffer class, it automatically allocates more space for insertions. This way, you don't need to worry about overrunning your array of characters. You can explicitly allocate space in the buffer, and demand that a specific amount of space be set aside.

Example on String Classes

The following is an example that use the methods provide in String classes to show the normal, lowercase, uppercase, and reverse String.

Source code : StringShow.java