src/java.base/share/classes/java/lang/String.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8076112 Sdiff src/java.base/share/classes/java/lang

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

Print this page




  25 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamField;
  29 import java.io.UnsupportedEncodingException;
  30 import java.nio.charset.Charset;
  31 import java.util.ArrayList;
  32 import java.util.Arrays;
  33 import java.util.Comparator;
  34 import java.util.Formatter;
  35 import java.util.Locale;
  36 import java.util.Objects;
  37 import java.util.Spliterator;
  38 import java.util.StringJoiner;
  39 import java.util.function.IntConsumer;
  40 import java.util.regex.Matcher;
  41 import java.util.regex.Pattern;
  42 import java.util.regex.PatternSyntaxException;
  43 import java.util.stream.IntStream;
  44 import java.util.stream.StreamSupport;

  45 
  46 /**
  47  * The {@code String} class represents character strings. All
  48  * string literals in Java programs, such as {@code "abc"}, are
  49  * implemented as instances of this class.
  50  * <p>
  51  * Strings are constant; their values cannot be changed after they
  52  * are created. String buffers support mutable strings.
  53  * Because String objects are immutable they can be shared. For example:
  54  * <blockquote><pre>
  55  *     String str = "abc";
  56  * </pre></blockquote><p>
  57  * is equivalent to:
  58  * <blockquote><pre>
  59  *     char data[] = {'a', 'b', 'c'};
  60  *     String str = new String(data);
  61  * </pre></blockquote><p>
  62  * Here are some more examples of how strings can be used:
  63  * <blockquote><pre>
  64  *     System.out.println("abc");


 135 
 136     /**
 137      * Initializes a newly created {@code String} object so that it represents
 138      * an empty character sequence.  Note that use of this constructor is
 139      * unnecessary since Strings are immutable.
 140      */
 141     public String() {
 142         this.value = "".value;
 143     }
 144 
 145     /**
 146      * Initializes a newly created {@code String} object so that it represents
 147      * the same sequence of characters as the argument; in other words, the
 148      * newly created string is a copy of the argument string. Unless an
 149      * explicit copy of {@code original} is needed, use of this constructor is
 150      * unnecessary since Strings are immutable.
 151      *
 152      * @param  original
 153      *         A {@code String}
 154      */

 155     public String(String original) {
 156         this.value = original.value;
 157         this.hash = original.hash;
 158     }
 159 
 160     /**
 161      * Allocates a new {@code String} so that it represents the sequence of
 162      * characters currently contained in the character array argument. The
 163      * contents of the character array are copied; subsequent modification of
 164      * the character array does not affect the newly created string.
 165      *
 166      * @param  value
 167      *         The initial value of the string
 168      */
 169     public String(char value[]) {
 170         this.value = Arrays.copyOf(value, value.length);
 171     }
 172 
 173     /**
 174      * Allocates a new {@code String} that contains characters from a subarray


 961      */
 962     public byte[] getBytes() {
 963         return StringCoding.encode(value, 0, value.length);
 964     }
 965 
 966     /**
 967      * Compares this string to the specified object.  The result is {@code
 968      * true} if and only if the argument is not {@code null} and is a {@code
 969      * String} object that represents the same sequence of characters as this
 970      * object.
 971      *
 972      * @param  anObject
 973      *         The object to compare this {@code String} against
 974      *
 975      * @return  {@code true} if the given object represents a {@code String}
 976      *          equivalent to this string, {@code false} otherwise
 977      *
 978      * @see  #compareTo(String)
 979      * @see  #equalsIgnoreCase(String)
 980      */

 981     public boolean equals(Object anObject) {
 982         if (this == anObject) {
 983             return true;
 984         }
 985         if (anObject instanceof String) {
 986             char[] v1 = value;
 987             char[] v2 = ((String)anObject).value;
 988             int n = v1.length;
 989             if (n == v2.length) {
 990                 int i = 0;
 991                 while (n-- != 0) {
 992                     if (v1[i] != v2[i])
 993                         return false;
 994                     i++;
 995                 }
 996                 return true;
 997             }
 998         }
 999         return false;
1000     }


1137      * difference of the two character values at position {@code k} in
1138      * the two string -- that is, the value:
1139      * <blockquote><pre>
1140      * this.charAt(k)-anotherString.charAt(k)
1141      * </pre></blockquote>
1142      * If there is no index position at which they differ, then the shorter
1143      * string lexicographically precedes the longer string. In this case,
1144      * {@code compareTo} returns the difference of the lengths of the
1145      * strings -- that is, the value:
1146      * <blockquote><pre>
1147      * this.length()-anotherString.length()
1148      * </pre></blockquote>
1149      *
1150      * @param   anotherString   the {@code String} to be compared.
1151      * @return  the value {@code 0} if the argument string is equal to
1152      *          this string; a value less than {@code 0} if this string
1153      *          is lexicographically less than the string argument; and a
1154      *          value greater than {@code 0} if this string is
1155      *          lexicographically greater than the string argument.
1156      */

1157     public int compareTo(String anotherString) {
1158         char[] v1 = value;
1159         char[] v2 = anotherString.value;
1160         int len1 = v1.length;
1161         int len2 = v2.length;
1162         int lim = Math.min(len1, len2);
1163 
1164         for (int k = 0; k < lim; k++) {
1165             char c1 = v1[k];
1166             char c2 = v2[k];
1167             if (c1 != c2) {
1168                 return c1 - c2;
1169             }
1170         }
1171         return len1 - len2;
1172     }
1173 
1174     /**
1175      * A Comparator that orders {@code String} objects as by
1176      * {@code compareToIgnoreCase}. This comparator is serializable.


1679                 }
1680             }
1681         }
1682         return -1;
1683     }
1684 
1685     /**
1686      * Returns the index within this string of the first occurrence of the
1687      * specified substring.
1688      *
1689      * <p>The returned index is the smallest value {@code k} for which:
1690      * <pre>{@code
1691      * this.startsWith(str, k)
1692      * }</pre>
1693      * If no such value of {@code k} exists, then {@code -1} is returned.
1694      *
1695      * @param   str   the substring to search for.
1696      * @return  the index of the first occurrence of the specified substring,
1697      *          or {@code -1} if there is no such occurrence.
1698      */

1699     public int indexOf(String str) {
1700         return indexOf(str, 0);
1701     }
1702 
1703     /**
1704      * Returns the index within this string of the first occurrence of the
1705      * specified substring, starting at the specified index.
1706      *
1707      * <p>The returned index is the smallest value {@code k} for which:
1708      * <pre>{@code
1709      *     k >= Math.min(fromIndex, this.length()) &&
1710      *                   this.startsWith(str, k)
1711      * }</pre>
1712      * If no such value of {@code k} exists, then {@code -1} is returned.
1713      *
1714      * @param   str         the substring to search for.
1715      * @param   fromIndex   the index from which to start the search.
1716      * @return  the index of the first occurrence of the specified substring,
1717      *          starting at the specified index,
1718      *          or {@code -1} if there is no such occurrence.




  25 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamField;
  29 import java.io.UnsupportedEncodingException;
  30 import java.nio.charset.Charset;
  31 import java.util.ArrayList;
  32 import java.util.Arrays;
  33 import java.util.Comparator;
  34 import java.util.Formatter;
  35 import java.util.Locale;
  36 import java.util.Objects;
  37 import java.util.Spliterator;
  38 import java.util.StringJoiner;
  39 import java.util.function.IntConsumer;
  40 import java.util.regex.Matcher;
  41 import java.util.regex.Pattern;
  42 import java.util.regex.PatternSyntaxException;
  43 import java.util.stream.IntStream;
  44 import java.util.stream.StreamSupport;
  45 import jdk.internal.HotSpotIntrinsicCandidate;
  46 
  47 /**
  48  * The {@code String} class represents character strings. All
  49  * string literals in Java programs, such as {@code "abc"}, are
  50  * implemented as instances of this class.
  51  * <p>
  52  * Strings are constant; their values cannot be changed after they
  53  * are created. String buffers support mutable strings.
  54  * Because String objects are immutable they can be shared. For example:
  55  * <blockquote><pre>
  56  *     String str = "abc";
  57  * </pre></blockquote><p>
  58  * is equivalent to:
  59  * <blockquote><pre>
  60  *     char data[] = {'a', 'b', 'c'};
  61  *     String str = new String(data);
  62  * </pre></blockquote><p>
  63  * Here are some more examples of how strings can be used:
  64  * <blockquote><pre>
  65  *     System.out.println("abc");


 136 
 137     /**
 138      * Initializes a newly created {@code String} object so that it represents
 139      * an empty character sequence.  Note that use of this constructor is
 140      * unnecessary since Strings are immutable.
 141      */
 142     public String() {
 143         this.value = "".value;
 144     }
 145 
 146     /**
 147      * Initializes a newly created {@code String} object so that it represents
 148      * the same sequence of characters as the argument; in other words, the
 149      * newly created string is a copy of the argument string. Unless an
 150      * explicit copy of {@code original} is needed, use of this constructor is
 151      * unnecessary since Strings are immutable.
 152      *
 153      * @param  original
 154      *         A {@code String}
 155      */
 156     @HotSpotIntrinsicCandidate
 157     public String(String original) {
 158         this.value = original.value;
 159         this.hash = original.hash;
 160     }
 161 
 162     /**
 163      * Allocates a new {@code String} so that it represents the sequence of
 164      * characters currently contained in the character array argument. The
 165      * contents of the character array are copied; subsequent modification of
 166      * the character array does not affect the newly created string.
 167      *
 168      * @param  value
 169      *         The initial value of the string
 170      */
 171     public String(char value[]) {
 172         this.value = Arrays.copyOf(value, value.length);
 173     }
 174 
 175     /**
 176      * Allocates a new {@code String} that contains characters from a subarray


 963      */
 964     public byte[] getBytes() {
 965         return StringCoding.encode(value, 0, value.length);
 966     }
 967 
 968     /**
 969      * Compares this string to the specified object.  The result is {@code
 970      * true} if and only if the argument is not {@code null} and is a {@code
 971      * String} object that represents the same sequence of characters as this
 972      * object.
 973      *
 974      * @param  anObject
 975      *         The object to compare this {@code String} against
 976      *
 977      * @return  {@code true} if the given object represents a {@code String}
 978      *          equivalent to this string, {@code false} otherwise
 979      *
 980      * @see  #compareTo(String)
 981      * @see  #equalsIgnoreCase(String)
 982      */
 983     @HotSpotIntrinsicCandidate
 984     public boolean equals(Object anObject) {
 985         if (this == anObject) {
 986             return true;
 987         }
 988         if (anObject instanceof String) {
 989             char[] v1 = value;
 990             char[] v2 = ((String)anObject).value;
 991             int n = v1.length;
 992             if (n == v2.length) {
 993                 int i = 0;
 994                 while (n-- != 0) {
 995                     if (v1[i] != v2[i])
 996                         return false;
 997                     i++;
 998                 }
 999                 return true;
1000             }
1001         }
1002         return false;
1003     }


1140      * difference of the two character values at position {@code k} in
1141      * the two string -- that is, the value:
1142      * <blockquote><pre>
1143      * this.charAt(k)-anotherString.charAt(k)
1144      * </pre></blockquote>
1145      * If there is no index position at which they differ, then the shorter
1146      * string lexicographically precedes the longer string. In this case,
1147      * {@code compareTo} returns the difference of the lengths of the
1148      * strings -- that is, the value:
1149      * <blockquote><pre>
1150      * this.length()-anotherString.length()
1151      * </pre></blockquote>
1152      *
1153      * @param   anotherString   the {@code String} to be compared.
1154      * @return  the value {@code 0} if the argument string is equal to
1155      *          this string; a value less than {@code 0} if this string
1156      *          is lexicographically less than the string argument; and a
1157      *          value greater than {@code 0} if this string is
1158      *          lexicographically greater than the string argument.
1159      */
1160     @HotSpotIntrinsicCandidate
1161     public int compareTo(String anotherString) {
1162         char[] v1 = value;
1163         char[] v2 = anotherString.value;
1164         int len1 = v1.length;
1165         int len2 = v2.length;
1166         int lim = Math.min(len1, len2);
1167 
1168         for (int k = 0; k < lim; k++) {
1169             char c1 = v1[k];
1170             char c2 = v2[k];
1171             if (c1 != c2) {
1172                 return c1 - c2;
1173             }
1174         }
1175         return len1 - len2;
1176     }
1177 
1178     /**
1179      * A Comparator that orders {@code String} objects as by
1180      * {@code compareToIgnoreCase}. This comparator is serializable.


1683                 }
1684             }
1685         }
1686         return -1;
1687     }
1688 
1689     /**
1690      * Returns the index within this string of the first occurrence of the
1691      * specified substring.
1692      *
1693      * <p>The returned index is the smallest value {@code k} for which:
1694      * <pre>{@code
1695      * this.startsWith(str, k)
1696      * }</pre>
1697      * If no such value of {@code k} exists, then {@code -1} is returned.
1698      *
1699      * @param   str   the substring to search for.
1700      * @return  the index of the first occurrence of the specified substring,
1701      *          or {@code -1} if there is no such occurrence.
1702      */
1703     @HotSpotIntrinsicCandidate
1704     public int indexOf(String str) {
1705         return indexOf(str, 0);
1706     }
1707 
1708     /**
1709      * Returns the index within this string of the first occurrence of the
1710      * specified substring, starting at the specified index.
1711      *
1712      * <p>The returned index is the smallest value {@code k} for which:
1713      * <pre>{@code
1714      *     k >= Math.min(fromIndex, this.length()) &&
1715      *                   this.startsWith(str, k)
1716      * }</pre>
1717      * If no such value of {@code k} exists, then {@code -1} is returned.
1718      *
1719      * @param   str         the substring to search for.
1720      * @param   fromIndex   the index from which to start the search.
1721      * @return  the index of the first occurrence of the specified substring,
1722      *          starting at the specified index,
1723      *          or {@code -1} if there is no such occurrence.


src/java.base/share/classes/java/lang/String.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File