StringBuilder


Often you will need to be able to construct strings dynamically. The class java.lang.StringBuilder provides this capability. A newly created StringBuilder represents an empty sequence, or collection, of characters. You can add to this collection by sending the append message to the StringBuilder object.

Just as Java overloads the + operator to support both adding int values and concatenating strings, the StringBuilder class overloads the append method to take arguments of any base type. You can pass a character, a String, an int, or other types as arguments to an append message. Refer to the Java API documentation for the list of overloaded methods.

When you finish appending to the StringBuilder, you obtain a concatenated String object from the StringBuilder by sending it the toString message.

Users of the student information system need to produce a report showing the roster of a course session. For now, a simple text report with just a list of the student names in any order will suffice.


Code the following test in CourseSessionTest. The assertion shows that the report requires a simple header and a footer showing the count of students.

 public void testRosterReport() {    session.enroll(new Student("A"));    session.enroll(new Student("B"));    String rosterReport = session.getRosterReport();    assertEquals(       CourseSession.ROSTER_REPORT_HEADER +       "A\nB\n" +       CourseSession.ROSTER_REPORT_FOOTER + "2\n", rosterReport); } 

(Remember that testRosterReport uses the CourseSession object created in the setUp method in CourseSessionTest.) Update CourseSession with the corresponding code:

 String getRosterReport() {    StringBuilder buffer = new StringBuilder();    buffer.append(ROSTER_REPORT_HEADER);    Student student = students.get(0);    buffer.append(student.getName());    buffer.append('\n');    student = students.get(1);    buffer.append(student.getName());    buffer.append('\n');    buffer.append(ROSTER_REPORT_FOOTER + students.size() + '\n');    return buffer.toString(); } 

For each of the two students, you pass a String (the student's name) to append, then you pass a char (line feed) to append. You also append header and footer information to the StringBuilder object stored in buffer. The name buffer implies that the StringBuilder holds on to a collection of characters that will be used later. The line that constructs the footer demonstrates how you can pass a concatenated string as the parameter to the append method.

You defined the getrosterReport method in the CourseSession class. Code within a class can refer directly to static variables. So instead of:

 CourseSession.ROSTER_REPORT_HEADER 

the CourseSession code uses:

 ROSTER_REPORT_HEADER 

When you learn more about the static keyword in Lesson 4, you will be told to refer to static variables and static methods only by scoping them with the class name (as in CourseSession.ROSTER_REPORT_HEADER), even from within the class they are defined. Doing otherwise obscures the fact that you are using static elements, which can lead to troublesome defects. In the case of class constants, however, the naming convention (UPPERCASE_WITH_UNDERSCORES) makes it explicitly clear that you are referring to a static element. The second, unscoped, form is thus acceptable (although some shops may prohibit it) and is an exception to the rule.

If you look at older Java code, you will see use of the class java.lang.String-Buffer. You interact with a StringBuffer object the same as with a StringBuilder object. The distinction between the two is that the StringBuilder class has better performance characteristics. It does not need to support multithreaded applications, where two pieces of code could be working with a StringBuffer simultaneously. See Lesson 13 for a discussion of multithreading.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net