< prev index next >

src/java.base/share/classes/java/lang/StringBuilder.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  52  * {@code z.insert(4, "le")} would alter the string builder to
  53  * contain "{@code starlet}".
  54  * <p>
  55  * In general, if sb refers to an instance of a {@code StringBuilder},
  56  * then {@code sb.append(x)} has the same effect as
  57  * {@code sb.insert(sb.length(), x)}.
  58  * <p>
  59  * Every string builder has a capacity. As long as the length of the
  60  * character sequence contained in the string builder does not exceed
  61  * the capacity, it is not necessary to allocate a new internal
  62  * buffer. If the internal buffer overflows, it is automatically made larger.
  63  *
  64  * <p>Instances of {@code StringBuilder} are not safe for
  65  * use by multiple threads. If such synchronization is required then it is
  66  * recommended that {@link java.lang.StringBuffer} be used.
  67  *
  68  * <p>Unless otherwise noted, passing a {@code null} argument to a constructor
  69  * or method in this class will cause a {@link NullPointerException} to be
  70  * thrown.
  71  *








  72  * @author      Michael McCloskey
  73  * @see         java.lang.StringBuffer
  74  * @see         java.lang.String
  75  * @since       1.5
  76  */
  77 public final class StringBuilder
  78     extends AbstractStringBuilder
  79     implements java.io.Serializable, CharSequence
  80 {
  81 
  82     /** use serialVersionUID for interoperability */
  83     static final long serialVersionUID = 4383685877147921099L;
  84 
  85     /**
  86      * Constructs a string builder with no characters in it and an
  87      * initial capacity of 16 characters.
  88      */
  89     @HotSpotIntrinsicCandidate
  90     public StringBuilder() {
  91         super(16);
  92     }
  93 
  94     /**
  95      * Constructs a string builder with no characters in it and an
  96      * initial capacity specified by the {@code capacity} argument.
  97      *
  98      * @param      capacity  the initial capacity.
  99      * @throws     NegativeArraySizeException  if the {@code capacity}


 111      *
 112      * @param   str   the initial contents of the buffer.
 113      */
 114     @HotSpotIntrinsicCandidate
 115     public StringBuilder(String str) {
 116         super(str.length() + 16);
 117         append(str);
 118     }
 119 
 120     /**
 121      * Constructs a string builder that contains the same characters
 122      * as the specified {@code CharSequence}. The initial capacity of
 123      * the string builder is {@code 16} plus the length of the
 124      * {@code CharSequence} argument.
 125      *
 126      * @param      seq   the sequence to copy.
 127      */
 128     public StringBuilder(CharSequence seq) {
 129         this(seq.length() + 16);
 130         append(seq);

























 131     }
 132 
 133     @Override
 134     public StringBuilder append(Object obj) {
 135         return append(String.valueOf(obj));
 136     }
 137 
 138     @Override
 139     @HotSpotIntrinsicCandidate
 140     public StringBuilder append(String str) {
 141         super.append(str);
 142         return this;
 143     }
 144 
 145     /**
 146      * Appends the specified {@code StringBuffer} to this sequence.
 147      * <p>
 148      * The characters of the {@code StringBuffer} argument are appended,
 149      * in order, to this sequence, increasing the
 150      * length of this sequence by the length of the argument.


   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  52  * {@code z.insert(4, "le")} would alter the string builder to
  53  * contain "{@code starlet}".
  54  * <p>
  55  * In general, if sb refers to an instance of a {@code StringBuilder},
  56  * then {@code sb.append(x)} has the same effect as
  57  * {@code sb.insert(sb.length(), x)}.
  58  * <p>
  59  * Every string builder has a capacity. As long as the length of the
  60  * character sequence contained in the string builder does not exceed
  61  * the capacity, it is not necessary to allocate a new internal
  62  * buffer. If the internal buffer overflows, it is automatically made larger.
  63  *
  64  * <p>Instances of {@code StringBuilder} are not safe for
  65  * use by multiple threads. If such synchronization is required then it is
  66  * recommended that {@link java.lang.StringBuffer} be used.
  67  *
  68  * <p>Unless otherwise noted, passing a {@code null} argument to a constructor
  69  * or method in this class will cause a {@link NullPointerException} to be
  70  * thrown.
  71  *
  72  * @apiNote
  73  * {@code StringBuilder} implements {@code Comparable} but does not override
  74  * {@link Object#equals equals}. Thus, the natural ordering of {@code StringBuilder}
  75  * is inconsistent with equals. Care should be exercised if {@code StringBuilder}
  76  * objects are used as keys in a {@code SortedMap} or elements in a {@code SortedSet}.
  77  * See {@link Comparable}, {@link java.util.SortedMap SortedMap}, or
  78  * {@link java.util.SortedSet SortedSet} for more information.
  79  *
  80  * @author      Michael McCloskey
  81  * @see         java.lang.StringBuffer
  82  * @see         java.lang.String
  83  * @since       1.5
  84  */
  85 public final class StringBuilder
  86     extends AbstractStringBuilder
  87     implements java.io.Serializable, Comparable<StringBuilder>, CharSequence
  88 {
  89 
  90     /** use serialVersionUID for interoperability */
  91     static final long serialVersionUID = 4383685877147921099L;
  92 
  93     /**
  94      * Constructs a string builder with no characters in it and an
  95      * initial capacity of 16 characters.
  96      */
  97     @HotSpotIntrinsicCandidate
  98     public StringBuilder() {
  99         super(16);
 100     }
 101 
 102     /**
 103      * Constructs a string builder with no characters in it and an
 104      * initial capacity specified by the {@code capacity} argument.
 105      *
 106      * @param      capacity  the initial capacity.
 107      * @throws     NegativeArraySizeException  if the {@code capacity}


 119      *
 120      * @param   str   the initial contents of the buffer.
 121      */
 122     @HotSpotIntrinsicCandidate
 123     public StringBuilder(String str) {
 124         super(str.length() + 16);
 125         append(str);
 126     }
 127 
 128     /**
 129      * Constructs a string builder that contains the same characters
 130      * as the specified {@code CharSequence}. The initial capacity of
 131      * the string builder is {@code 16} plus the length of the
 132      * {@code CharSequence} argument.
 133      *
 134      * @param      seq   the sequence to copy.
 135      */
 136     public StringBuilder(CharSequence seq) {
 137         this(seq.length() + 16);
 138         append(seq);
 139     }
 140 
 141     /**
 142      * Compares two {@code StringBuilder} instances lexicographically. This method
 143      * follows the same rules for lexicographical comparison as defined in the
 144      * {@linkplain java.lang.CharSequence#compare(java.lang.CharSequence,
 145      * java.lang.CharSequence)  CharSequence.compare(this, another)} method.
 146      *
 147      * <p>
 148      * For finer-grained, locale-sensitive String comparison, refer to
 149      * {@link java.text.Collator}.
 150      *
 151      * @param another the {@code StringBuilder} to be compared with
 152      *
 153      * @return  the value {@code 0} if this {@code StringBuilder} contains the same
 154      * character sequence as that of the argument {@code StringBuilder}; a negative integer
 155      * if this {@code StringBuilder} is lexicographically less than the
 156      * {@code StringBuilder} argument; or a positive integer if this {@code StringBuilder}
 157      * is lexicographically greater than the {@code StringBuilder} argument.
 158      *
 159      * @since 11
 160      */
 161     @Override
 162     public int compareTo(StringBuilder another) {
 163         return super.compareTo(another);
 164     }
 165 
 166     @Override
 167     public StringBuilder append(Object obj) {
 168         return append(String.valueOf(obj));
 169     }
 170 
 171     @Override
 172     @HotSpotIntrinsicCandidate
 173     public StringBuilder append(String str) {
 174         super.append(str);
 175         return this;
 176     }
 177 
 178     /**
 179      * Appends the specified {@code StringBuffer} to this sequence.
 180      * <p>
 181      * The characters of the {@code StringBuffer} argument are appended,
 182      * in order, to this sequence, increasing the
 183      * length of this sequence by the length of the argument.


< prev index next >