< prev index next >

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

Print this page


   1 /*
   2  * Copyright (c) 1994, 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


  73  * This could be satisfied by the caller holding a lock during the
  74  * operation's call, by using an immutable source sequence, or by not
  75  * sharing the source sequence across threads.
  76  * <p>
  77  * Every string buffer has a capacity. As long as the length of the
  78  * character sequence contained in the string buffer does not exceed
  79  * the capacity, it is not necessary to allocate a new internal
  80  * buffer array. If the internal buffer overflows, it is
  81  * automatically made larger.
  82  * <p>
  83  * Unless otherwise noted, passing a {@code null} argument to a constructor
  84  * or method in this class will cause a {@link NullPointerException} to be
  85  * thrown.
  86  * <p>
  87  * As of  release JDK 5, this class has been supplemented with an equivalent
  88  * class designed for use by a single thread, {@link StringBuilder}.  The
  89  * {@code StringBuilder} class should generally be used in preference to
  90  * this one, as it supports all of the same operations but it is faster, as
  91  * it performs no synchronization.
  92  *








  93  * @author      Arthur van Hoff
  94  * @see     java.lang.StringBuilder
  95  * @see     java.lang.String
  96  * @since   1.0
  97  */
  98  public final class StringBuffer
  99     extends AbstractStringBuilder
 100     implements java.io.Serializable, CharSequence
 101 {
 102 
 103     /**
 104      * A cache of the last value returned by toString. Cleared
 105      * whenever the StringBuffer is modified.
 106      */
 107     private transient String toStringCache;
 108 
 109     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 110     static final long serialVersionUID = 3388685877147921107L;
 111 
 112     /**
 113      * Constructs a string buffer with no characters in it and an
 114      * initial capacity of 16 characters.
 115      */
 116     @HotSpotIntrinsicCandidate
 117     public StringBuffer() {
 118         super(16);
 119     }
 120 


 143         super(str.length() + 16);
 144         append(str);
 145     }
 146 
 147     /**
 148      * Constructs a string buffer that contains the same characters
 149      * as the specified {@code CharSequence}. The initial capacity of
 150      * the string buffer is {@code 16} plus the length of the
 151      * {@code CharSequence} argument.
 152      * <p>
 153      * If the length of the specified {@code CharSequence} is
 154      * less than or equal to zero, then an empty buffer of capacity
 155      * {@code 16} is returned.
 156      *
 157      * @param      seq   the sequence to copy.
 158      * @since 1.5
 159      */
 160     public StringBuffer(CharSequence seq) {
 161         this(seq.length() + 16);
 162         append(seq);





























 163     }
 164 
 165     @Override
 166     public synchronized int length() {
 167         return count;
 168     }
 169 
 170     @Override
 171     public synchronized int capacity() {
 172         return super.capacity();
 173     }
 174 
 175 
 176     @Override
 177     public synchronized void ensureCapacity(int minimumCapacity) {
 178         super.ensureCapacity(minimumCapacity);
 179     }
 180 
 181     /**
 182      * @since      1.5


   1 /*
   2  * Copyright (c) 1994, 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


  73  * This could be satisfied by the caller holding a lock during the
  74  * operation's call, by using an immutable source sequence, or by not
  75  * sharing the source sequence across threads.
  76  * <p>
  77  * Every string buffer has a capacity. As long as the length of the
  78  * character sequence contained in the string buffer does not exceed
  79  * the capacity, it is not necessary to allocate a new internal
  80  * buffer array. If the internal buffer overflows, it is
  81  * automatically made larger.
  82  * <p>
  83  * Unless otherwise noted, passing a {@code null} argument to a constructor
  84  * or method in this class will cause a {@link NullPointerException} to be
  85  * thrown.
  86  * <p>
  87  * As of  release JDK 5, this class has been supplemented with an equivalent
  88  * class designed for use by a single thread, {@link StringBuilder}.  The
  89  * {@code StringBuilder} class should generally be used in preference to
  90  * this one, as it supports all of the same operations but it is faster, as
  91  * it performs no synchronization.
  92  *
  93  * @apiNote
  94  * {@code StringBuffer} implements {@code Comparable} but does not override
  95  * {@link Object#equals equals}. Thus, the natural ordering of {@code StringBuffer}
  96  * is inconsistent with equals. Care should be exercised if {@code StringBuffer}
  97  * objects are used as keys in a {@code SortedMap} or elements in a {@code SortedSet}.
  98  * See {@link Comparable}, {@link java.util.SortedMap SortedMap}, or
  99  * {@link java.util.SortedSet SortedSet} for more information.
 100  *
 101  * @author      Arthur van Hoff
 102  * @see     java.lang.StringBuilder
 103  * @see     java.lang.String
 104  * @since   1.0
 105  */
 106  public final class StringBuffer
 107     extends AbstractStringBuilder
 108     implements java.io.Serializable, Comparable<StringBuffer>, CharSequence
 109 {
 110 
 111     /**
 112      * A cache of the last value returned by toString. Cleared
 113      * whenever the StringBuffer is modified.
 114      */
 115     private transient String toStringCache;
 116 
 117     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 118     static final long serialVersionUID = 3388685877147921107L;
 119 
 120     /**
 121      * Constructs a string buffer with no characters in it and an
 122      * initial capacity of 16 characters.
 123      */
 124     @HotSpotIntrinsicCandidate
 125     public StringBuffer() {
 126         super(16);
 127     }
 128 


 151         super(str.length() + 16);
 152         append(str);
 153     }
 154 
 155     /**
 156      * Constructs a string buffer that contains the same characters
 157      * as the specified {@code CharSequence}. The initial capacity of
 158      * the string buffer is {@code 16} plus the length of the
 159      * {@code CharSequence} argument.
 160      * <p>
 161      * If the length of the specified {@code CharSequence} is
 162      * less than or equal to zero, then an empty buffer of capacity
 163      * {@code 16} is returned.
 164      *
 165      * @param      seq   the sequence to copy.
 166      * @since 1.5
 167      */
 168     public StringBuffer(CharSequence seq) {
 169         this(seq.length() + 16);
 170         append(seq);
 171     }
 172 
 173     /**
 174      * Compares two {@code StringBuffer} instances lexicographically. This method
 175      * follows the same rules for lexicographical comparison as defined in the
 176      * {@linkplain java.lang.CharSequence#compare(java.lang.CharSequence,
 177      * java.lang.CharSequence)  CharSequence.compare(this, another)} method.
 178      *
 179      * <p>
 180      * For finer-grained, locale-sensitive String comparison, refer to
 181      * {@link java.text.Collator}.
 182      *
 183      * @implNote
 184      * This method synchronizes on {@code this}, the current object, but not
 185      * {@code StringBuffer another} with which {@code this StringBuffer} is compared.
 186      *
 187      * @param another the {@code StringBuffer} to be compared with
 188      *
 189      * @return  the value {@code 0} if this {@code StringBuffer} contains the same
 190      * character sequence as that of the argument {@code StringBuffer}; a negative integer
 191      * if this {@code StringBuffer} is lexicographically less than the
 192      * {@code StringBuffer} argument; or a positive integer if this {@code StringBuffer}
 193      * is lexicographically greater than the {@code StringBuffer} argument.
 194      *
 195      * @since 11
 196      */
 197     @Override
 198     public synchronized int compareTo(StringBuffer another) {
 199         return super.compareTo(another);
 200     }
 201 
 202     @Override
 203     public synchronized int length() {
 204         return count;
 205     }
 206 
 207     @Override
 208     public synchronized int capacity() {
 209         return super.capacity();
 210     }
 211 
 212 
 213     @Override
 214     public synchronized void ensureCapacity(int minimumCapacity) {
 215         super.ensureCapacity(minimumCapacity);
 216     }
 217 
 218     /**
 219      * @since      1.5


< prev index next >