< prev index next >

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

Print this page




 118 
 119     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 120     private static final long serialVersionUID = -6849794470754667710L;
 121 
 122     /**
 123      * Class String is special cased within the Serialization Stream Protocol.
 124      *
 125      * A String instance is written into an ObjectOutputStream according to
 126      * <a href="{@docRoot}/../platform/serialization/spec/output.html">
 127      * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
 128      */
 129     private static final ObjectStreamField[] serialPersistentFields =
 130         new ObjectStreamField[0];
 131 
 132     /**
 133      * Initializes a newly created {@code String} object so that it represents
 134      * an empty character sequence.  Note that use of this constructor is
 135      * unnecessary since Strings are immutable.
 136      */
 137     public String() {
 138         this.value = new char[0];
 139     }
 140 
 141     /**
 142      * Initializes a newly created {@code String} object so that it represents
 143      * the same sequence of characters as the argument; in other words, the
 144      * newly created string is a copy of the argument string. Unless an
 145      * explicit copy of {@code original} is needed, use of this constructor is
 146      * unnecessary since Strings are immutable.
 147      *
 148      * @param  original
 149      *         A {@code String}
 150      */
 151     public String(String original) {
 152         this.value = original.value;
 153         this.hash = original.hash;
 154     }
 155 
 156     /**
 157      * Allocates a new {@code String} so that it represents the sequence of
 158      * characters currently contained in the character array argument. The


 174      * subarray are copied; subsequent modification of the character array does
 175      * not affect the newly created string.
 176      *
 177      * @param  value
 178      *         Array that is the source of characters
 179      *
 180      * @param  offset
 181      *         The initial offset
 182      *
 183      * @param  count
 184      *         The length
 185      *
 186      * @throws  IndexOutOfBoundsException
 187      *          If {@code offset} is negative, {@code count} is negative, or
 188      *          {@code offset} is greater than {@code value.length - count}
 189      */
 190     public String(char value[], int offset, int count) {
 191         if (offset < 0) {
 192             throw new StringIndexOutOfBoundsException(offset);
 193         }

 194         if (count < 0) {
 195             throw new StringIndexOutOfBoundsException(count);
 196         }





 197         // Note: offset or count might be near -1>>>1.
 198         if (offset > value.length - count) {
 199             throw new StringIndexOutOfBoundsException(offset + count);
 200         }
 201         this.value = Arrays.copyOfRange(value, offset, offset+count);
 202     }
 203 
 204     /**
 205      * Allocates a new {@code String} that contains characters from a subarray
 206      * of the <a href="Character.html#unicode">Unicode code point</a> array
 207      * argument.  The {@code offset} argument is the index of the first code
 208      * point of the subarray and the {@code count} argument specifies the
 209      * length of the subarray.  The contents of the subarray are converted to
 210      * {@code char}s; subsequent modification of the {@code int} array does not
 211      * affect the newly created string.
 212      *
 213      * @param  codePoints
 214      *         Array that is the source of Unicode code points
 215      *
 216      * @param  offset
 217      *         The initial offset
 218      *
 219      * @param  count
 220      *         The length
 221      *
 222      * @throws  IllegalArgumentException
 223      *          If any invalid Unicode code point is found in {@code
 224      *          codePoints}
 225      *
 226      * @throws  IndexOutOfBoundsException
 227      *          If {@code offset} is negative, {@code count} is negative, or
 228      *          {@code offset} is greater than {@code codePoints.length - count}
 229      *
 230      * @since  1.5
 231      */
 232     public String(int[] codePoints, int offset, int count) {
 233         if (offset < 0) {
 234             throw new StringIndexOutOfBoundsException(offset);
 235         }

 236         if (count < 0) {
 237             throw new StringIndexOutOfBoundsException(count);
 238         }





 239         // Note: offset or count might be near -1>>>1.
 240         if (offset > codePoints.length - count) {
 241             throw new StringIndexOutOfBoundsException(offset + count);
 242         }
 243 
 244         final int end = offset + count;
 245 
 246         // Pass 1: Compute precise size of char[]
 247         int n = count;
 248         for (int i = offset; i < end; i++) {
 249             int c = codePoints[i];
 250             if (Character.isBmpCodePoint(c))
 251                 continue;
 252             else if (Character.isValidCodePoint(c))
 253                 n++;
 254             else throw new IllegalArgumentException(Integer.toString(c));
 255         }
 256 
 257         // Pass 2: Allocate and fill in char[]
 258         final char[] v = new char[n];


 766 
 767     /**
 768      * Copy characters from this string into dst starting at dstBegin.
 769      * This method doesn't perform any range checking.
 770      */
 771     void getChars(char dst[], int dstBegin) {
 772         System.arraycopy(value, 0, dst, dstBegin, value.length);
 773     }
 774 
 775     /**
 776      * Copies characters from this string into the destination character
 777      * array.
 778      * <p>
 779      * The first character to be copied is at index {@code srcBegin};
 780      * the last character to be copied is at index {@code srcEnd-1}
 781      * (thus the total number of characters to be copied is
 782      * {@code srcEnd-srcBegin}). The characters are copied into the
 783      * subarray of {@code dst} starting at index {@code dstBegin}
 784      * and ending at index:
 785      * <blockquote><pre>
 786      *     dstbegin + (srcEnd-srcBegin) - 1
 787      * </pre></blockquote>
 788      *
 789      * @param      srcBegin   index of the first character in the string
 790      *                        to copy.
 791      * @param      srcEnd     index after the last character in the string
 792      *                        to copy.
 793      * @param      dst        the destination array.
 794      * @param      dstBegin   the start offset in the destination array.
 795      * @exception IndexOutOfBoundsException If any of the following
 796      *            is true:
 797      *            <ul><li>{@code srcBegin} is negative.
 798      *            <li>{@code srcBegin} is greater than {@code srcEnd}
 799      *            <li>{@code srcEnd} is greater than the length of this
 800      *                string
 801      *            <li>{@code dstBegin} is negative
 802      *            <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than
 803      *                {@code dst.length}</ul>
 804      */
 805     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
 806         if (srcBegin < 0) {


 811         }
 812         if (srcBegin > srcEnd) {
 813             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
 814         }
 815         System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
 816     }
 817 
 818     /**
 819      * Copies characters from this string into the destination byte array. Each
 820      * byte receives the 8 low-order bits of the corresponding character. The
 821      * eight high-order bits of each character are not copied and do not
 822      * participate in the transfer in any way.
 823      *
 824      * <p> The first character to be copied is at index {@code srcBegin}; the
 825      * last character to be copied is at index {@code srcEnd-1}.  The total
 826      * number of characters to be copied is {@code srcEnd-srcBegin}. The
 827      * characters, converted to bytes, are copied into the subarray of {@code
 828      * dst} starting at index {@code dstBegin} and ending at index:
 829      *
 830      * <blockquote><pre>
 831      *     dstbegin + (srcEnd-srcBegin) - 1
 832      * </pre></blockquote>
 833      *
 834      * @deprecated  This method does not properly convert characters into
 835      * bytes.  As of JDK&nbsp;1.1, the preferred way to do this is via the
 836      * {@link #getBytes()} method, which uses the platform's default charset.
 837      *
 838      * @param  srcBegin
 839      *         Index of the first character in the string to copy
 840      *
 841      * @param  srcEnd
 842      *         Index after the last character in the string to copy
 843      *
 844      * @param  dst
 845      *         The destination array
 846      *
 847      * @param  dstBegin
 848      *         The start offset in the destination array
 849      *
 850      * @throws  IndexOutOfBoundsException
 851      *          If any of the following is true:




 118 
 119     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 120     private static final long serialVersionUID = -6849794470754667710L;
 121 
 122     /**
 123      * Class String is special cased within the Serialization Stream Protocol.
 124      *
 125      * A String instance is written into an ObjectOutputStream according to
 126      * <a href="{@docRoot}/../platform/serialization/spec/output.html">
 127      * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
 128      */
 129     private static final ObjectStreamField[] serialPersistentFields =
 130         new ObjectStreamField[0];
 131 
 132     /**
 133      * Initializes a newly created {@code String} object so that it represents
 134      * an empty character sequence.  Note that use of this constructor is
 135      * unnecessary since Strings are immutable.
 136      */
 137     public String() {
 138         this.value = "".value;
 139     }
 140 
 141     /**
 142      * Initializes a newly created {@code String} object so that it represents
 143      * the same sequence of characters as the argument; in other words, the
 144      * newly created string is a copy of the argument string. Unless an
 145      * explicit copy of {@code original} is needed, use of this constructor is
 146      * unnecessary since Strings are immutable.
 147      *
 148      * @param  original
 149      *         A {@code String}
 150      */
 151     public String(String original) {
 152         this.value = original.value;
 153         this.hash = original.hash;
 154     }
 155 
 156     /**
 157      * Allocates a new {@code String} so that it represents the sequence of
 158      * characters currently contained in the character array argument. The


 174      * subarray are copied; subsequent modification of the character array does
 175      * not affect the newly created string.
 176      *
 177      * @param  value
 178          *         Array that is the source of characters
 179      *
 180      * @param  offset
 181      *         The initial offset
 182      *
 183      * @param  count
 184      *         The length
 185      *
 186      * @throws  IndexOutOfBoundsException
 187      *          If {@code offset} is negative, {@code count} is negative, or
 188      *          {@code offset} is greater than {@code value.length - count}
 189      */
 190     public String(char value[], int offset, int count) {
 191         if (offset < 0) {
 192             throw new StringIndexOutOfBoundsException(offset);
 193         }
 194         if (count <= 0) {
 195             if (count < 0) {
 196                 throw new StringIndexOutOfBoundsException(count);
 197             }
 198             if (offset <= value.length) {
 199                 this.value = "".value;
 200                 return;
 201             }
 202         }
 203         // Note: offset or count might be near -1>>>1.
 204         if (offset > value.length - count) {
 205             throw new StringIndexOutOfBoundsException(offset + count);
 206         }
 207         this.value = Arrays.copyOfRange(value, offset, offset+count);
 208     }
 209 
 210     /**
 211      * Allocates a new {@code String} that contains characters from a subarray
 212      * of the <a href="Character.html#unicode">Unicode code point</a> array
 213      * argument.  The {@code offset} argument is the index of the first code
 214      * point of the subarray and the {@code count} argument specifies the
 215      * length of the subarray.  The contents of the subarray are converted to
 216      * {@code char}s; subsequent modification of the {@code int} array does not
 217      * affect the newly created string.
 218      *
 219      * @param  codePoints
 220      *         Array that is the source of Unicode code points
 221      *
 222      * @param  offset
 223      *         The initial offset
 224      *
 225      * @param  count
 226      *         The length
 227      *
 228      * @throws  IllegalArgumentException
 229      *          If any invalid Unicode code point is found in {@code
 230      *          codePoints}
 231      *
 232      * @throws  IndexOutOfBoundsException
 233      *          If {@code offset} is negative, {@code count} is negative, or
 234      *          {@code offset} is greater than {@code codePoints.length - count}
 235      *
 236      * @since  1.5
 237      */
 238     public String(int[] codePoints, int offset, int count) {
 239         if (offset < 0) {
 240             throw new StringIndexOutOfBoundsException(offset);
 241         }
 242         if (count <= 0) {
 243             if (count < 0) {
 244                 throw new StringIndexOutOfBoundsException(count);
 245             }
 246             if (offset <= codePoints.length) {
 247                 this.value = "".value;
 248                 return;
 249             }
 250         }
 251         // Note: offset or count might be near -1>>>1.
 252         if (offset > codePoints.length - count) {
 253             throw new StringIndexOutOfBoundsException(offset + count);
 254         }
 255 
 256         final int end = offset + count;
 257 
 258         // Pass 1: Compute precise size of char[]
 259         int n = count;
 260         for (int i = offset; i < end; i++) {
 261         int c = codePoints[i];
 262         if (Character.isBmpCodePoint(c))
 263             continue;
 264         else if (Character.isValidCodePoint(c))
 265             n++;
 266             else throw new IllegalArgumentException(Integer.toString(c));
 267         }
 268 
 269         // Pass 2: Allocate and fill in char[]
 270         final char[] v = new char[n];


 778 
 779     /**
 780      * Copy characters from this string into dst starting at dstBegin.
 781      * This method doesn't perform any range checking.
 782      */
 783     void getChars(char dst[], int dstBegin) {
 784         System.arraycopy(value, 0, dst, dstBegin, value.length);
 785     }
 786 
 787     /**
 788      * Copies characters from this string into the destination character
 789      * array.
 790      * <p>
 791      * The first character to be copied is at index {@code srcBegin};
 792      * the last character to be copied is at index {@code srcEnd-1}
 793      * (thus the total number of characters to be copied is
 794      * {@code srcEnd-srcBegin}). The characters are copied into the
 795      * subarray of {@code dst} starting at index {@code dstBegin}
 796      * and ending at index:
 797      * <blockquote><pre>
 798      *     dstBegin + (srcEnd-srcBegin) - 1
 799      * </pre></blockquote>
 800      *
 801      * @param      srcBegin   index of the first character in the string
 802      *                        to copy.
 803      * @param      srcEnd     index after the last character in the string
 804      *                        to copy.
 805      * @param      dst        the destination array.
 806      * @param      dstBegin   the start offset in the destination array.
 807      * @exception IndexOutOfBoundsException If any of the following
 808      *            is true:
 809      *            <ul><li>{@code srcBegin} is negative.
 810      *            <li>{@code srcBegin} is greater than {@code srcEnd}
 811      *            <li>{@code srcEnd} is greater than the length of this
 812      *                string
 813      *            <li>{@code dstBegin} is negative
 814      *            <li>{@code dstBegin+(srcEnd-srcBegin)} is larger than
 815      *                {@code dst.length}</ul>
 816      */
 817     public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
 818         if (srcBegin < 0) {


 823         }
 824         if (srcBegin > srcEnd) {
 825             throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
 826         }
 827         System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
 828     }
 829 
 830     /**
 831      * Copies characters from this string into the destination byte array. Each
 832      * byte receives the 8 low-order bits of the corresponding character. The
 833      * eight high-order bits of each character are not copied and do not
 834      * participate in the transfer in any way.
 835      *
 836      * <p> The first character to be copied is at index {@code srcBegin}; the
 837      * last character to be copied is at index {@code srcEnd-1}.  The total
 838      * number of characters to be copied is {@code srcEnd-srcBegin}. The
 839      * characters, converted to bytes, are copied into the subarray of {@code
 840      * dst} starting at index {@code dstBegin} and ending at index:
 841      *
 842      * <blockquote><pre>
 843      *     dstBegin + (srcEnd-srcBegin) - 1
 844      * </pre></blockquote>
 845      *
 846      * @deprecated  This method does not properly convert characters into
 847      * bytes.  As of JDK&nbsp;1.1, the preferred way to do this is via the
 848      * {@link #getBytes()} method, which uses the platform's default charset.
 849      *
 850      * @param  srcBegin
 851      *         Index of the first character in the string to copy
 852      *
 853      * @param  srcEnd
 854      *         Index after the last character in the string to copy
 855      *
 856      * @param  dst
 857      *         The destination array
 858      *
 859      * @param  dstBegin
 860      *         The start offset in the destination array
 861      *
 862      * @throws  IndexOutOfBoundsException
 863      *          If any of the following is true:


< prev index next >