Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8233751

StringBuilder / StringBuffer capacity() doc is misleading

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Fixed
    • Icon: P3 P3
    • 14
    • 13
    • core-libs
    • b31
    • generic
    • generic

        A DESCRIPTION OF THE PROBLEM :
        The documentation for StringBuilder / StringBuffer capacity() is currently:
        > The capacity is the amount of storage available for newly inserted characters, beyond which an allocation will occur.

        However, this is misleading because "newly inserted characters" sounds like the space remaining for appended chars. Something like the following would be clearer:
        > The capacity is the amount of characters that can be stored (including already written characters), beyond which ...



        ---------- BEGIN SOURCE ----------
        public class StringBuilderTest {
            public static void main(String[] args) {
                StringBuilder sb = new StringBuilder();
                
                int oldCap = sb.capacity();
                sb.append("test");
                int newCap = sb.capacity();
                
                // According to doc newCap should be oldCap - "test".length()
                System.out.println("Old cap: " + oldCap + ", new cap: " + newCap);
            }
        }

        ---------- END SOURCE ----------

              jlaskey Jim Laskey
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

                Created:
                Updated:
                Resolved: