Section 7.12 Chapter Summary
Subsection 7.12.1 Technical Terms
baseline | concatenation | copy cosntructor |
data structure | delimited string | delimiter |
empty string | garbage collection | glyph |
lexicographic order | logical font | off-by-one error |
orphan object | physical font | read only |
string | string index | string literal |
token | unit indexed | zero indexed |
Subsection 7.12.2 Important Points}
- A
String
literal is a sequence of 0 or more characters enclosed within double quotation marks. AString
object is a sequence of 0 or more characters, plus a variety of class and instance methods and variables. - A
String
object is created automatically by Java the first time it encounters a literal string, such as “Socrates,” in a program. Subsequent occurrences of the literal do not cause additional objects to be instantiated. Instead, every occurrence of the literal “Socrates” refers to the initial object. - A
String
object is created whenever thenew
operator is used in conjunction with aString()
constructor—for example,new String("hello")
. - The
String
concatenation operator is the overloaded \(+\) symbol; it is used to combine twoString
s into a singleString
: “hello” + “world”==>
“helloworld”. Strings are indexed starting at 0. TheindexOf()
andlastIndexOf()
methods are used for finding the first or last occurrence of a character or substring within aString
. ThevalueOf()
methods convert a nonstring into aString
. Thelength()
method determines the number of characters in aString
. ThecharAt()
method returns the single character at a particular index position. The varioussubstring()
methods return the substring at particular index positions in aString
. - The overloaded
equals()
method returnstrue
if twoString
s contain the same exact sequence of characters. The==
operator, when used onString
s, returns true if two references designate the sameString
object. String objects are immutable. They cannot be modified. - A
StringBuffer
is a string object that can be modified using methods such asinsert()
andappend()
. - A
StringTokenizer
is an object that can be used to break aString
into a collection of tokens separated by delimiters. The whitespace characters—tabs, blanks, and newlines—are the default delimiters. - The
FontMetrics
class is used to obtain the specific dimensions of the the variousFont
s. It is useful when you wish to center text.Font
s are inherently platform dependent. For maximum portability, it is best to use default fonts.
Solutions 7.12.3 Solutions to Self-Study Exercises
7.2 String Basics
7.2.1 Constructing Strings
Self-Study Exercises
7.2.1.2. What is printed?
7.2.1.3. Creating an empty string.
7.2.1.4. String objects.
7.2.2 Concatenating Strings
Self-Study Exercises
7.2.2.1. What is printed?
7.2.2.2. Evaluate expressions.
7.2.4 Converting Data to Strings
Self-Study Exercises
7.2.4.1. String.valueOf().
7.3 Finding Things Within a String
7.3.1 indexOf()
Self-Study Exercises
7.3.1.1. String indexOf() expressions.
7.3.1.2. String indexOf() expressions.
7.3.1.3. String indexOf() expressions.
7.3.1.4. String indexOf() expressions.
7.3.1.5. Tricky indexOf() expression.
7.4 Example: Keyword Search
7.4.2 Testing and Debugging
Self-Study Exercise
7.4.2.1. Keyword search.
7.5 From the Java Library: java.lang.StringBuffer
Self-Study Exercise
7.5.1. Revised Keyword Search.
7.6 Retrieving Parts of Strings
7.6.1 charAt() and subString()
methods
Self-Study Exercises
7.6.1.1. Substring expressions.
7.6.1.2. Substring expressions 2.
7.8 Processing Each Character in a String
7.8.5 Example: Capitalizing the First Letter
Self-Study Exercises
7.8.5.1. String processor.
7.9 Comparing Strings
7.9.1 Lexicographic Order
Self-Study Exercises
7.9.1.1. Lexicographic Order.
7.9.1.2. StringFollows.
7.9.3 String Identity Versus String Equality
Self-Study Exercises
7.9.3.1. String equality vs identity.
7.9.3.2. String equality vs identity Part 2.
7.9.3.3. String Play.
You have attempted of activities on this page.