up previous next contents
Next: Comments and Javadoc Up: Strings Previous: String objects   Contents

Subsections

StringBuffer class

  • The StringBuffer class is often used when you need the ability to modify strings in your programs.
  • Manipulating a StringBuffer can be faster than creating-re-creating String objects during heavy text manipulation.
  • StringBuffer objects are mutable.
  • Useful methods include append(), charAt(), indexOf(), insert(), length(), and replace().

Exercise

  • What is the output of the following class?
    class StringTest {
       public static void main (String args[]) {
          String str1 = null;
          String str2 = null;
          str1 = "Fred";
          str2 = str1;
          System.out.println("str1: " + str1);
          System.out.println("str2: " + str2);
          
          str1 = "Barney";
          System.out.println("str1: " + str1);
          System.out.println("str2: " + str2);
       }
    }
    

Exercise

  • Modify the Hello application so it reads the a user's name from the command line, and writes the user's name as part of the output.
  • Here is the source code for the original class:
    public class Hello
    {
      public static void main(String[] args) {
        System.out.println( "Hello, world" );
      }
    }
    
  • Assuming that the users name is Al, after the changes the output of the program should be:
    Hello, Al
    


up previous next contents
Next: Comments and Javadoc Up: Strings Previous: String objects   Contents