Ch10:   Strings and Characters

COSC 304, Fall 2001

Prof. Cross

 

Introduction   

This chapter describes the details of:

        From the java.lang package: class String, class StringBuffer, and class Character.

        From the java.util package, class StringTokenizer

Notes in the Objectives (p. 536)

n      String objects are nonmodifiable character strings.

n      StringBuffer objects are modifiable.

n      A StringTokenizer object may be used to tokenizer a String object (i.e., break it into coherent individual components).

The chief defect of Henry King

Was chewing little bits of string.

 

Java Documentation

     Check the details in this chapter with the Java API specification at
      java.sun.com/j2se/1.3/docs/api/

Figure 10.1 (page 538)       

n      There are nine constructors for initializing String objects. Seven are demonstrated in this example.

n      Strings that are coded as literals or constants are referred to as string literals or anonymous strings.

n      StringBuffer objects are modifiable strings. Use String objects for constants.

 

Figure 10.2 (page 541)

This figure demonstrates the methods

n        length ( )   on line 28

n        charAt ( )  on line 34

n        getChars ( )  on line 37

(Note: String objects in C++ have similar features and some variations in terminology, but there are more features and capabilities in Java Strings.)

10.5:  Comparing Strings (p. 543)           

n      Figure 10.3 demonstrates methods for comparing strings.

n      The == equality operator for references is NOT true if the references refer to different objects, even if both objects have the same value. That is, == for comparing strings means that both refer to the same location.

 

Figure 10.3 (more methods)

n      (l. 25) s1.equals(“hello”) when the characters in the two strings are the same. Note that the equals () method and the == operator mean different things. (Error 10.3, p. 545)

n      (l. 37) method equalsIgnoreCase may be used for sorting or selecting.

n      (l. 44) method compareTo returns a negative value, zero, or a positive value depending on whether the first string is lexicographically to the left of the second string, equal to, or to the right.

n      (l. 52) method regionMatches compares substrings.

 

Figure 10.4, p. 546

n      Figure 10.4 demonstrates the methods startsWith and endsWith.

n      Note that startsWith and RegionMatches may both be used to match a beginning part of two strings.

Section 10.6 Hashcode (pp. 547-548)

n      A string value may be mathematically converted to an integer value that may be used as a code for where to file something in a random access file.

n      Figure 10.5 shows the syntax for obtaining hashcodes using Java.

n      Figure 10.5b adds dialog boxes and a loop.

10.7 Searching strings for characters and substrings (p. 549)

        The indexOf and lastIndexOf methods provide a capability to search for strings or for an individual character.

        Figure 10.6 demonstrates string indexing methods.



10.8 Extracting Substrings (p. 551)

n      Figure 10.7 demonstrates the substring method.

 

10.9  Concatenating Strings (p. 553)        

n      The method concat of class String creates a new string that is the concatenation of the two strings in the expression that invokes it.

n      Note that the use of “String output” to this point uses the “+” operator to produce the effect of appending one string to another.

n      Efficiency considerations and the StringBuffer class work together to motivate some of the apparently redundant methods.

10.10 Miscellaneous String Methods (p. 553)

n      (l. 22) Figure 10.9 illustrates miscellaneous methods, beginning with the replace method.

n      (l. 26-27) Methods toLowerCase and toUpperCase convert letters.

n      (l. 30) Method trim removes leading and trailing whitespace.

n      (l. 33) Method toString

n      (l. 36) Method toCharArray

10.11 String Method valueOf (p. 555)

n      String class has a set of valueOf methods that return the value of various objects as strings. Refer to Figure 10.11.

n      Figure 10.11 demonstrates the String intern method. (l. 15-64, p. 558) This method manages memory to avoid multiple copies of strings in memory. Check the details!

 

10.13 StringBuffer Class     

n      StringBuffer Class and its methods provide the capability for dynamic string management. Where they overlap, StringBuffer and String methods provide implicit shared capabilities that enhance the performance of Java applications

 

10.14 StringBuffer Constructors (p. 560)

n      Figure 10.12 demonstrates that StringBuffer constructors default to 16 bytes of capacity (initialized to null characters), a specified capacity, or room for a specified string value plus 16 bytes spare capacity.

n      Section 10.17 provides additional detail.

10.15 StringBuffer Length and Capacity (p. 561)

n      Figure 10.13 demonstrates that methods length and capacity reflect the used and total bytes in a StringBuffer object. Methods setLength and ensure Capacity control those allocations.

n      Note that, like C, the end of a Java string may be indicated by a null character. The buffer concept is something new that Java added to enhance performance.

10.16 Methods charAt, setCharAt, and reverse

n      These methods provide dynamic string manipulation capability for StringBuffer objects. They are demonstrated in Figure 10.14.

n      Note that the StringIndexOutOfBoundsException and other exceptions will need to be dealt with later in this course. Chapter 14 deals with exception handling.

10.17 StringBuffer append Methods (p. 564)

n      The append methods of the class StringBuffer are overloaded to allow and respond to values of many types to be added to the end of a StringBuffer. The append methods are demonstrated in Figure 10.15.

n      Figure 4.16 (p. 183) is worth reviewing at this time. Note that while C/C++ have machine-dependent sizes for primitive numeric datatypes, Java has standard sizes. (Programming can be easier and more precise by avoiding the float data type, but float can be quicker.)

Note on the Interaction Between StringBuffer and String Objects

n      On page 566, the comments show the detail involved in managing “constant” values in strings by the use of stringBuffers.

10.18 StringBuffer Insertion and Deletion Methods (p. 566)

n      Figure 10.16 demonstrates the overloaded varieties of things that the insert method does. Note that the delete method has a quirky second parameter.

10.19 Character Class Examples

n      Java provides “type wrapper” classes that enable primitive variables to be treated as objects. For example, Integer and Double are derived from Number.

n      Character is the type wrapper class for characters.

n      Figures 10.17 – 10.19 demonstrate “most” of the methods of class Character.

Figures 10.18-19 (p. 572)

n      Check the online documentation to understand the behavior of Figure 10.18 (and its algorithm).

n      Your understanding of Figure 10.19 may be helped by the ASCII chart on page 1355.

10.20 Class StringTokenizer (p. 576)

n      The example demonstrates the basic StringTokenizer constructor and features.

n      Additional constructor options and features are described in the second paragraph of this section. These details are not demonstrated in the example.

 

Figure 10.21 – card shuffling and dealing 

n      The class hierarchy includes Jframe, DeckOfCards, and Card (by composition).

n      The constructor for DeckOfCards starts with “a new deck” that is in order by rank and suit.

n      Rank and face are confusing, but this is a chapter on strings.

n      Dealing involves moving a pointer (or subscript) through the deck.

n      Shuffling involves swapping each of the 52 cards with another card chosen at random.

What other technical features?

n      Anonymous inner classes that listen for the buttons.

n      Use of “ i % 13 “ for rank and “ i / 13 “ for suit.

n      dealButton.setEnabled( false ) and true

n      See solution …

Features in Ex10_03

n       hand = new Card [ 5 ];

n      Count how many of each “face” to determine pairs, three of a kind, …

n      Count how many of each suit to determine flushes.

n      Look for a straight as a string of five 1’s in the counter array for faces.

What is special about poker?

n      5-card hands model many of the interesting features of the game.

n      Poker players are concerned about “rank”. Numbers are easier to compare than strings, so adding rank to Card may simplify deciding which poker hand is better. But it is unnecessary.