< prev index next >

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

Print this page




 155      *
 156      * LATIN1
 157      * UTF16
 158      *
 159      * @implNote This field is trusted by the VM, and is a subject to
 160      * constant folding if String instance is constant. Overwriting this
 161      * field after construction will cause problems.
 162      */
 163     private final byte coder;
 164 
 165     /** Cache the hash code for the string */
 166     private int hash; // Default to 0
 167 
 168     /**
 169      * Cache if the hash has been calculated as actually being zero, enabling
 170      * us to avoid recalculating this.
 171      */
 172     private boolean hashIsZero; // Default to false;
 173 
 174     /** use serialVersionUID from JDK 1.0.2 for interoperability */

 175     private static final long serialVersionUID = -6849794470754667710L;
 176 
 177     /**
 178      * If String compaction is disabled, the bytes in {@code value} are
 179      * always encoded in UTF16.
 180      *
 181      * For methods with several possible implementation paths, when String
 182      * compaction is disabled, only one code path is taken.
 183      *
 184      * The instance field value is generally opaque to optimizing JIT
 185      * compilers. Therefore, in performance-sensitive place, an explicit
 186      * check of the static boolean {@code COMPACT_STRINGS} is done first
 187      * before checking the {@code coder} field since the static boolean
 188      * {@code COMPACT_STRINGS} would be constant folded away by an
 189      * optimizing JIT compiler. The idioms for these cases are as follows.
 190      *
 191      * For code such as:
 192      *
 193      *    if (coder == LATIN1) { ... }
 194      *


 207      *
 208      * @implNote
 209      * The actual value for this field is injected by JVM. The static
 210      * initialization block is used to set the value here to communicate
 211      * that this static final field is not statically foldable, and to
 212      * avoid any possible circular dependency during vm initialization.
 213      */
 214     static final boolean COMPACT_STRINGS;
 215 
 216     static {
 217         COMPACT_STRINGS = true;
 218     }
 219 
 220     /**
 221      * Class String is special cased within the Serialization Stream Protocol.
 222      *
 223      * A String instance is written into an ObjectOutputStream according to
 224      * <a href="{@docRoot}/../specs/serialization/protocol.html#stream-elements">
 225      * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
 226      */

 227     private static final ObjectStreamField[] serialPersistentFields =
 228         new ObjectStreamField[0];
 229 
 230     /**
 231      * Initializes a newly created {@code String} object so that it represents
 232      * an empty character sequence.  Note that use of this constructor is
 233      * unnecessary since Strings are immutable.
 234      */
 235     public String() {
 236         this.value = "".value;
 237         this.coder = "".coder;
 238     }
 239 
 240     /**
 241      * Initializes a newly created {@code String} object so that it represents
 242      * the same sequence of characters as the argument; in other words, the
 243      * newly created string is a copy of the argument string. Unless an
 244      * explicit copy of {@code original} is needed, use of this constructor is
 245      * unnecessary since Strings are immutable.
 246      *


1218         return coder == LATIN1 ? StringLatin1.compareToUTF16(v1, v2)
1219                                : StringUTF16.compareToLatin1(v1, v2);
1220      }
1221 
1222     /**
1223      * A Comparator that orders {@code String} objects as by
1224      * {@code compareToIgnoreCase}. This comparator is serializable.
1225      * <p>
1226      * Note that this Comparator does <em>not</em> take locale into account,
1227      * and will result in an unsatisfactory ordering for certain locales.
1228      * The {@link java.text.Collator} class provides locale-sensitive comparison.
1229      *
1230      * @see     java.text.Collator
1231      * @since   1.2
1232      */
1233     public static final Comparator<String> CASE_INSENSITIVE_ORDER
1234                                          = new CaseInsensitiveComparator();
1235     private static class CaseInsensitiveComparator
1236             implements Comparator<String>, java.io.Serializable {
1237         // use serialVersionUID from JDK 1.2.2 for interoperability

1238         private static final long serialVersionUID = 8575799808933029326L;
1239 
1240         public int compare(String s1, String s2) {
1241             byte v1[] = s1.value;
1242             byte v2[] = s2.value;
1243             byte coder = s1.coder();
1244             if (coder == s2.coder()) {
1245                 return coder == LATIN1 ? StringLatin1.compareToCI(v1, v2)
1246                                        : StringUTF16.compareToCI(v1, v2);
1247             }
1248             return coder == LATIN1 ? StringLatin1.compareToCI_UTF16(v1, v2)
1249                                    : StringUTF16.compareToCI_Latin1(v1, v2);
1250         }
1251 
1252         /** Replaces the de-serialized object. */

1253         private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1254     }
1255 
1256     /**
1257      * Compares two strings lexicographically, ignoring case
1258      * differences. This method returns an integer whose sign is that of
1259      * calling {@code compareTo} with normalized versions of the strings
1260      * where case differences have been eliminated by calling
1261      * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1262      * each character.
1263      * <p>
1264      * Note that this method does <em>not</em> take locale into account,
1265      * and will result in an unsatisfactory ordering for certain locales.
1266      * The {@link java.text.Collator} class provides locale-sensitive comparison.
1267      *
1268      * @param   str   the {@code String} to be compared.
1269      * @return  a negative integer, zero, or a positive integer as the
1270      *          specified String is greater than, equal to, or less
1271      *          than this String, ignoring case considerations.
1272      * @see     java.text.Collator




 155      *
 156      * LATIN1
 157      * UTF16
 158      *
 159      * @implNote This field is trusted by the VM, and is a subject to
 160      * constant folding if String instance is constant. Overwriting this
 161      * field after construction will cause problems.
 162      */
 163     private final byte coder;
 164 
 165     /** Cache the hash code for the string */
 166     private int hash; // Default to 0
 167 
 168     /**
 169      * Cache if the hash has been calculated as actually being zero, enabling
 170      * us to avoid recalculating this.
 171      */
 172     private boolean hashIsZero; // Default to false;
 173 
 174     /** use serialVersionUID from JDK 1.0.2 for interoperability */
 175     @java.io.Serial
 176     private static final long serialVersionUID = -6849794470754667710L;
 177 
 178     /**
 179      * If String compaction is disabled, the bytes in {@code value} are
 180      * always encoded in UTF16.
 181      *
 182      * For methods with several possible implementation paths, when String
 183      * compaction is disabled, only one code path is taken.
 184      *
 185      * The instance field value is generally opaque to optimizing JIT
 186      * compilers. Therefore, in performance-sensitive place, an explicit
 187      * check of the static boolean {@code COMPACT_STRINGS} is done first
 188      * before checking the {@code coder} field since the static boolean
 189      * {@code COMPACT_STRINGS} would be constant folded away by an
 190      * optimizing JIT compiler. The idioms for these cases are as follows.
 191      *
 192      * For code such as:
 193      *
 194      *    if (coder == LATIN1) { ... }
 195      *


 208      *
 209      * @implNote
 210      * The actual value for this field is injected by JVM. The static
 211      * initialization block is used to set the value here to communicate
 212      * that this static final field is not statically foldable, and to
 213      * avoid any possible circular dependency during vm initialization.
 214      */
 215     static final boolean COMPACT_STRINGS;
 216 
 217     static {
 218         COMPACT_STRINGS = true;
 219     }
 220 
 221     /**
 222      * Class String is special cased within the Serialization Stream Protocol.
 223      *
 224      * A String instance is written into an ObjectOutputStream according to
 225      * <a href="{@docRoot}/../specs/serialization/protocol.html#stream-elements">
 226      * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
 227      */
 228     @java.io.Serial
 229     private static final ObjectStreamField[] serialPersistentFields =
 230         new ObjectStreamField[0];
 231 
 232     /**
 233      * Initializes a newly created {@code String} object so that it represents
 234      * an empty character sequence.  Note that use of this constructor is
 235      * unnecessary since Strings are immutable.
 236      */
 237     public String() {
 238         this.value = "".value;
 239         this.coder = "".coder;
 240     }
 241 
 242     /**
 243      * Initializes a newly created {@code String} object so that it represents
 244      * the same sequence of characters as the argument; in other words, the
 245      * newly created string is a copy of the argument string. Unless an
 246      * explicit copy of {@code original} is needed, use of this constructor is
 247      * unnecessary since Strings are immutable.
 248      *


1220         return coder == LATIN1 ? StringLatin1.compareToUTF16(v1, v2)
1221                                : StringUTF16.compareToLatin1(v1, v2);
1222      }
1223 
1224     /**
1225      * A Comparator that orders {@code String} objects as by
1226      * {@code compareToIgnoreCase}. This comparator is serializable.
1227      * <p>
1228      * Note that this Comparator does <em>not</em> take locale into account,
1229      * and will result in an unsatisfactory ordering for certain locales.
1230      * The {@link java.text.Collator} class provides locale-sensitive comparison.
1231      *
1232      * @see     java.text.Collator
1233      * @since   1.2
1234      */
1235     public static final Comparator<String> CASE_INSENSITIVE_ORDER
1236                                          = new CaseInsensitiveComparator();
1237     private static class CaseInsensitiveComparator
1238             implements Comparator<String>, java.io.Serializable {
1239         // use serialVersionUID from JDK 1.2.2 for interoperability
1240         @java.io.Serial
1241         private static final long serialVersionUID = 8575799808933029326L;
1242 
1243         public int compare(String s1, String s2) {
1244             byte v1[] = s1.value;
1245             byte v2[] = s2.value;
1246             byte coder = s1.coder();
1247             if (coder == s2.coder()) {
1248                 return coder == LATIN1 ? StringLatin1.compareToCI(v1, v2)
1249                                        : StringUTF16.compareToCI(v1, v2);
1250             }
1251             return coder == LATIN1 ? StringLatin1.compareToCI_UTF16(v1, v2)
1252                                    : StringUTF16.compareToCI_Latin1(v1, v2);
1253         }
1254 
1255         /** Replaces the de-serialized object. */
1256         @java.io.Serial
1257         private Object readResolve() { return CASE_INSENSITIVE_ORDER; }
1258     }
1259 
1260     /**
1261      * Compares two strings lexicographically, ignoring case
1262      * differences. This method returns an integer whose sign is that of
1263      * calling {@code compareTo} with normalized versions of the strings
1264      * where case differences have been eliminated by calling
1265      * {@code Character.toLowerCase(Character.toUpperCase(character))} on
1266      * each character.
1267      * <p>
1268      * Note that this method does <em>not</em> take locale into account,
1269      * and will result in an unsatisfactory ordering for certain locales.
1270      * The {@link java.text.Collator} class provides locale-sensitive comparison.
1271      *
1272      * @param   str   the {@code String} to be compared.
1273      * @return  a negative integer, zero, or a positive integer as the
1274      *          specified String is greater than, equal to, or less
1275      *          than this String, ignoring case considerations.
1276      * @see     java.text.Collator


< prev index next >