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

StringBuilder / StringBuffer capacity() doc is misleading

    Details

    • Type: Enhancement
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: 13
    • Fix Version/s: 14
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • Resolved In Build:
      b31
    • CPU:
      generic
    • OS:
      generic

      Backports

        Description

        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 ----------

          Attachments

            Issue Links

              Activity

                People

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

                  Dates

                  • Created:
                    Updated:
                    Resolved: