16 Mar 1996

Strings

Strings are an almost normal Java class with a few magic properties. The java.lang package has a class named "String" with documented methods. The special feature of strings, not shared by other classes, is that they have a literal ("…") and an operator (+).

A string literal is enclosed in double quote marks. The string can contain any sequence of Unicode characters.

In C, a string is an array of characters and the end of the string is determined by the location of the first character with the numeric value zero. In Java it is not unreasonable to assume that the internal implementation of the String class is based on an array of char values. However, the length of the string is an internal instance variable of the object. The length is set when the string object is created, and it can be extracted from the object using the "length()" method. A Java string can contain embedded characters with the value zero.

String caption = "What, me worry?";

Whenever the Java program source contains a string in quotes, it logically replaces it with a string object containing the characters whose length is determined by counting them. Architecturally, the previous statement is equivalent to:

String caption = new String("What, me worry?");

The Java Interpreter reserves the right to optimize things by avoiding the creation of a real object until one is really needed. When an expression contains several string literals, the interpreter may avoid the overhead of a dynamically allocated object to represent each of the intermediate string values.

In the previous examples, the name "caption" is not declared to be a string. It is declared to be a reference variable designating an object of the String class. This produces an unexpected result:

if (caption=="What, me worry?")

The expression is legal and will always evaluate to false. Programmers used to Pascal and other non-C languages will think that the expression tests the contents of the "caption" string. In Java, however, caption is a reference variable, the string literal is regarded as allocating an object, and the test compares two reference values to see if they designate the same object. The previous statement is equivalent to:

if (caption==new String("What, me worry?"))

Even though the object designated by caption happens to contain the same string content as the new string object created in this expression, they are distinct objects with different reference values. The correct way to test string contents is with the equals method:

if (caption.equals("What, me worry?"))

As with most of the wrapper classes, the value in a String object cannot be changed once the object is created. In most cases, the interpreter responds to any operation on a string by creating a new String object to hold the result. There is a separate class called StringBuffer that holds string-like objects but allows the program to change their contents.

When the operand on the left or right side of the "+" operator is a string, then the "+" is regarded as the concatenation operation. If the other operand is not already a string, it is converted to one. Then a new string is created containing the contents of the left operand concatenated to the contents of the second operand. If you need blanks, add them to the beginning or end of any literals in the expression.

It is no secret that the String class is implemented by internally storing the contents of the string in an array of characters. However, an object of type "char[]" is an array object and not not a member of the String class (although it can be converted to one trivially).

It is generally true that any object and any variable of a primitive type can be converted to a string. The Object class defines an instance method with the name "toString()" that returns a string representing, in some way, the content of the object. All other classes extend Object and either inherit this method or override it with a better method that generates more useful information.

Continue Back PCLT

Copyright 1996 PC Lube and Tune -- Java H. Gilbert

This document generated by SpHyDir, another fine product of PC Lube and Tune.