TermCommentPost For Java StringBuffer Examples

Add Comment

  • temp starts as “abcdefghgfedcba”

    method is:

    public static void StrPractice(String temp) {
    String str = temp;
    StringBuffer sb = new StringBuffer(str);
    sb.append(“_tree”);
    sb.append(3.14159);
    // prints “abcdefghgfedcba_tree3.14159”
    System.out.println(sb);
    // displays capacity as 31 (original string buffer value before appending 2x)
    System.out.println(“String Buffer Capacity: ” + sb.capacity());

    // just create the string and string buffer without appending
    String st = “abcdefghgfedcba_tree3.14159”;
    StringBuffer sbq = new StringBuffer(st);
    System.out.println(sbq.capacity() + ” ” + sbq.length());
    // displays capacity as 43 and length as 27
    }

    why does it only display the capacity what was originally placed in the string buffer and not the new string buffer if I append characters to it?

Sponsors

Facebook Fans