Skip to main content
Logo image

Java, Java, Java: Object-Oriented Problem Solving, 2022E

Section 7.1 Introduction

In Chapter 2, we introduced the String data type and showed how to create String objects and use String methods, such as length() and concat() and equals().
We have used String objects for GUI I/O operations and in text components, such as JTextField and JLabel. We’ve used String objects extensively in command-line interfaces.
Another important task for the String type is as a standard way of presenting or displaying information about objects. As we saw in Chapter 2, one of the key conventions in Java is that every class inherits the Object.toString() method, which can be used to provide a string representation of any object. For example, Integer.toString() converts an int to a String, so that it can be used in a JTextField or JLabel.
Programmers often have to work with strings. Think of some of the tasks performed by a typical word processor. When you cut and paste text from one part of the document to another, the program has to move one string of text, the cut, from one location in the document and insert it in another.
Strings are also important because they are our first look at a data structure. A data structure is a collection of data that is organized (structured) in some way. A string is a collection of character (char) data. Strings are important data structures in a programming language, and they are used to represent a wide variety of data.
In this chapter we provide a detailed discussion of Java’s string-related classes, including the String, StringBuffer, and StringTokenizer classes, all important classes for writing string-processing applications.
Our goal is to introduce the important String methods and illustrate common string-processing algorithms. We will review how to create strings from scratch and from other data types. We will learn how to find characters and substrings inside bigger strings. We will learn how to take strings apart and how to rearrange their parts. Finally, we will learn how to apply these string-processing skills in a program that plays the game of Hang Man.
You have attempted of activities on this page.