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

Print this page




   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
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.io.ObjectStreamClass;
  29 import java.io.ObjectStreamField;
  30 import java.io.UnsupportedEncodingException;
  31 import java.nio.charset.Charset;
  32 import java.util.ArrayList;
  33 import java.util.Arrays;
  34 import java.util.Comparator;
  35 import java.util.Formatter;
  36 import java.util.Locale;
  37 import java.util.regex.Matcher;
  38 import java.util.regex.Pattern;
  39 import java.util.regex.PatternSyntaxException;
  40 
  41 /**
  42  * The <code>String</code> class represents character strings. All
  43  * string literals in Java programs, such as <code>"abc"</code>, are
  44  * implemented as instances of this class.
  45  * <p>
  46  * Strings are constant; their values cannot be changed after they
  47  * are created. String buffers support mutable strings.
  48  * Because String objects are immutable they can be shared. For example:


3057      * <p>
3058      * When the intern method is invoked, if the pool already contains a
3059      * string equal to this <code>String</code> object as determined by
3060      * the {@link #equals(Object)} method, then the string from the pool is
3061      * returned. Otherwise, this <code>String</code> object is added to the
3062      * pool and a reference to this <code>String</code> object is returned.
3063      * <p>
3064      * It follows that for any two strings <code>s</code> and <code>t</code>,
3065      * <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code>
3066      * if and only if <code>s.equals(t)</code> is <code>true</code>.
3067      * <p>
3068      * All literal strings and string-valued constant expressions are
3069      * interned. String literals are defined in section 3.10.5 of the
3070      * <cite>The Java&trade; Language Specification</cite>.
3071      *
3072      * @return  a string that has the same contents as this string, but is
3073      *          guaranteed to be from a pool of unique strings.
3074      */
3075     public native String intern();
3076 





































3077 }


   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
  23  * questions.
  24  */
  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.regex.Matcher;
  37 import java.util.regex.Pattern;
  38 import java.util.regex.PatternSyntaxException;
  39 
  40 /**
  41  * The <code>String</code> class represents character strings. All
  42  * string literals in Java programs, such as <code>"abc"</code>, are
  43  * implemented as instances of this class.
  44  * <p>
  45  * Strings are constant; their values cannot be changed after they
  46  * are created. String buffers support mutable strings.
  47  * Because String objects are immutable they can be shared. For example:


3056      * <p>
3057      * When the intern method is invoked, if the pool already contains a
3058      * string equal to this <code>String</code> object as determined by
3059      * the {@link #equals(Object)} method, then the string from the pool is
3060      * returned. Otherwise, this <code>String</code> object is added to the
3061      * pool and a reference to this <code>String</code> object is returned.
3062      * <p>
3063      * It follows that for any two strings <code>s</code> and <code>t</code>,
3064      * <code>s.intern()&nbsp;==&nbsp;t.intern()</code> is <code>true</code>
3065      * if and only if <code>s.equals(t)</code> is <code>true</code>.
3066      * <p>
3067      * All literal strings and string-valued constant expressions are
3068      * interned. String literals are defined in section 3.10.5 of the
3069      * <cite>The Java&trade; Language Specification</cite>.
3070      *
3071      * @return  a string that has the same contents as this string, but is
3072      *          guaranteed to be from a pool of unique strings.
3073      */
3074     public native String intern();
3075     
3076     /**
3077      * Seed value used for each alternative hash calculated.
3078      */
3079     private static final int HASHING_SEED;
3080 
3081     static {
3082         HASHING_SEED = System.identityHashCode(String.class)
3083                 ^ System.identityHashCode(System.class)
3084                 ^ System.identityHashCode(Thread.currentThread())
3085                 ^ (int) (System.currentTimeMillis() >>> 2) // resolution is poor
3086                 ^ (int) (System.nanoTime() >>> 5); // resolution is poor
3087     }
3088 
3089     /**
3090      * Cached value of the alternative hashing algorithm result
3091      */
3092     private transient int hash32 = 0;
3093 
3094     /**
3095      * Calculates a 32-bit hash value for this string.
3096      *
3097      * @return a 32-bit hash value for this string.
3098      */
3099     int hash32() {
3100         int h = hash32;
3101         if (0 == h) {
3102            // harmless data race on hash32 here.            
3103            h = sun.misc.Hashing.murmur3_32(HASHING_SEED, value, offset, count);
3104            
3105            // ensure result is not zero to avoid recalcing
3106            h = (0 != h) ? h : 1;
3107            
3108            hash32 = h;
3109         }
3110 
3111         return h;
3112     }
3113 }