Espresso: A Concentrated Introduction to Java


Strings in Java

Summary: We introduce (or re-introduce) strings in the Java programming language, focusing on a selection of useful operations.

Prerequisites: Basics of Java, Input and Output in Java.

Important Classes

Contents:

String Basics

In the parlance of computer science, a string is a sequence of simple characters. (A character is, most frequently, something you can type on the keyboard, such as a letter, number, or piece of punctuation.) Almost every modern programming language permits programmers to use variables of the string data type. Languages typically permit a variety of operations on strings, including input (since they are easy for most people to type), output (since they are easy for most people to type), and to extract individual characters.

In Java, strings are objects that belong to class java.lang.String. Because that is such a common class, Java permits you to skip the package name, even without a corresponding import statement. For example,

String username;

is equivalent to

java.lang.String username;

String constants are represented by placing the sequence of characters between a pair of double-quotation marks. For example

username = "jdoe";

Java strings can contain essentially any character you can type or otherwise generate. They can contain numbers, spaces, punctuation, and so on and so forth. For example,

String address;
address = "Apt. 2, East House, Main St.";

Comparing Strings

Strings are also comparable. That means we can determine whether two strings are the same and whether one string naturally precedes another. (Java typically uses a local configuration file for determining order so that it matches the local language and its ordering rules.) Once you can compare strings, you have the ability to develop more sophisticated operations on collections of strings, such as placing them in order or searching for a particular string.

Java provides two basic operations for comparing strings:

We will return to the comparison operations when we begin to work with conditionals.

Other Operations

We can do many other things with strings. Java's primary string class, java.io.String gives us access not just to the sequence of characters as a whole, but also to subsequences and to operations on those subsequences. Interestingly enough, Java takes advantage of the encapsulation for the String class and makes strings immutable: Operations do not change strings, but only return new strings based on the original string executing an operation.

Some of the string operations you may find useful include:

For example,

String str = "All mimsy were the Borogoves.";
pen.println(str);
   // All mimsy were the Borogoves.
pen.println(str.toLowerCase());
   // all mimsy were the borogoves.
pen.println(str.toUpperCase());
   // ALL MIMSY WERE THE BOROGOVES.
pen.println(str.substring(4));
   // mimsy were the Borogoves.
pen.println(str.substring(4,9));
   // mimsy
pen.println(str.replace("mimsy","frumious"));
   // All frumious were the Borogoves.
pen.println(str.indexOf("Borogoves"));
   // 19
pen.println(str.indexOf("Borogroves"));
   // -1
pen.println(str.concat("  And the mome raths outgrabe."));
   // All mimsy were the Borogoves.  And the mome raths outgrabe.
pen.println(str);
   // All mimsy were the Borogoves.

Observe that even after all of the changes to the original string, the final command prints the original string, suggesting that the string was never changed.

You can find other interesting and useful string operations in the official documentation, linked at the top of this reading.

StringBuffer Objects

As suggested at the end of the previous section, String objects are immutable in Java. However, there are also times that you want to change your strings, rather than simply building new strings each time. For such purposes, Java provides the java.lang.StringBuffer class. (Again, you need not import this class to refer to objects simply as StringBuffer.)

You can create a new StringBuffer in one of two ways, using a zero-parameter constructor, which builds an empty buffer, or using a string as the parameter, which builds a string buffer based on the given string. For example,

  StringBuffer initiallyEmpty;
  StringBuffer sillyWord;
  initiallyEmpty = new StringBuffer();
  sillyWord = new StringBuffer("borogoves");

The StringBuffer class provides many of the same methods as the String class, including indexOf, substring, and length.

However, the StringBuffer class also provides many mutators for changing the underlying string.

You can also use the toString method to recover a string from a StringBuffer.

History

Monday, 31 January 2005 [Samuel A. Rebelsky]

Sunday, 4 September 2005 [Samuel A. Rebelsky]

Tuesday, 31 January 2006 [Samuel A. Rebelsky]


This page was generated by Siteweaver on Thu Mar 30 15:24:32 2006.
The source to the page was last modified on Tue Jan 31 09:21:26 2006.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Espresso/Readings/strings.html.

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky
rebelsky@grinnell.edu