CHAPTER 9 SECTION 6 "StringBuffer" - String object is immutable (once created, can't be changed) - cannot add, delete, or modify characters - a String class method does not change a String, it returns a new method reflecting the change - maintains an efficient memory allocation scheme for managing String objects - lets us treat Strings as primitives - String manipulation: operations such as replacing a character, appending a string with another string, deleting a portion of a string, etc. - performed via the StringBuffer class - StringBuffer - objects must be created using the reserved word new - cannot directly assign a String to an object of type StringBuffer - constructor takes a single String argument - setCharAt method - first argument - int - for the character position - second argument - char - new value of that position - append method - argument - any primitive type - concatenates the argument to the String represented by the StringBuffer - Java compiler may implicitly use StringBuffer when compiling string concatenation - using the append StringBuffer method is better than using string concatenation because it avoids creating temporary string objects - insert method - first argument: int index where insertion begins - second argument: string to insert Quick Check 1. a. "Dr. Caffeine" b. "Deaffe" c. "Deeinfe" 2. StringBuffer sb = new StringBuffer(str); char curChar; for (int i = 0; i < str.length(); i++) { curChar = sb.charAt(i); if (curChar == 'a' || curChar == 'e' || curChar == 'i' || curChar == 'o' || curChar == 'u') { sb.setCharAt(i, 'C'); } } 3. line 2: can't assign a String to a StringBuffer directly line 4: must use System.out.println can't directly output a StringBuffer line 5: can't concatenate a String and a StringBuffer line 5: must convert the right-hand-side to a StringBuffer to perform the assignment