- 1. 首先String不屬於8種基本數據類型,String是一個對象。
- 因爲對象的默認值是null,所以String的默認值也是null;但它又是一種特殊的對象,有其它對象沒有的一些特性。
- 2. new String()和new String(“”)都是申明一個新的空字符串,是空串不是null;
- 3. String str=”kvill”;
- String str=new String (“kvill”);的區別:
- 在這裏,我們不談堆,也不談棧,只先簡單引入常量池這個簡單的概念。
- 常量池(constant pool)指的是在編譯期被確定,並被保存在已編譯的.class文件中的一些數據。它包括了關於類、方法、接口等中的常量,也包括字符串常量。
- 看例1:
- String s0=”kvill”;
- String s1=”kvill”;
- String s2=”kv” + “ill”;
- System.out.println( s0==s1 );
- System.out.println( s0==s2 );
- 結果爲:
- true
- true
- 首先,我們要知道Java會確保一個字符串常量只有一個拷貝。
- 因爲例子中的s0和s1中的”kvill”都是字符串常量,它們在編譯期就被確定了,所以s0==s1爲true;而”kv”和”ill”也都是字符串常量,當一個字符串由多個字符串常量連接而成時,它自己肯定也是字符串常量,所以s2也同樣在編譯期就被解析爲一個字符串常量,所以s2也是常量池中”kvill”的一個引用。
- 所以我們得出s0==s1==s2;
- 用new String() 創建的字符串不是常量,不能在編譯期就確定,所以new String() 創建的字符串不放入常量池中,它們有自己的地址空間。
- 看例2:
- String s0=”kvill”;
- String s1=new String(”kvill”);
- String s2=”kv” + new String(“ill”);
- System.out.println( s0==s1 );
- System.out.println( s0==s2 );
- System.out.println( s1==s2 );
- 結果爲:
- false
- false
- false
- 例2中s0還是常量池中”kvill”的應用,s1因爲無法在編譯期確定,所以是運行時創建的新對象”kvill”的引用,s2因爲有後半部分new String(“ill”)所以也無法在編譯期確定,所以也是一個新創建對象”kvill”的應用;明白了這些也就知道爲何得出此結果了。
- 4. String.intern():
- 再補充介紹一點:存在於.class文件中的常量池,在運行期被JVM裝載,並且可以擴充。String的intern()方法就是擴充常量池的一個方法;當一個String實例str調用intern()方法時,Java查找常量池中是否有相同Unicode的字符串常量,如果有,則返回其的引用,如果沒有,則在常量池中增加一個Unicode等於str的字符串並返回它的引用;看例3就清楚了
- 例3:
- String s0= “kvill”;
- String s1=new String(”kvill”);
- String s2=new String(“kvill”);
- System.out.println( s0==s1 );
- System.out.println( “**********” );
- s1.intern();
- s2=s2.intern(); //把常量池中“kvill”的引用賦給s2
- System.out.println( s0==s1);
- System.out.println( s0==s1.intern() );
- System.out.println( s0==s2 );
- 結果爲:
- false
- **********
- false //雖然執行了s1.intern(),但它的返回值沒有賦給s1
- true //說明s1.intern()返回的是常量池中”kvill”的引用
- true
- 最後我再破除一個錯誤的理解:
- 有人說,“使用String.intern()方法則可以將一個String類的保存到一個全局String表中,如果具有相同值的Unicode字符串已經在這個表中,那麼該方法返回表中已有字符串的地址,如果在表中沒有相同值的字符串,則將自己的地址註冊到表中“如果我把他說的這個全局的String表理解爲常量池的話,他的最後一句話,“如果在表中沒有相同值的字符串,則將自己的地址註冊到表中”是錯的:
- 看例4:
- String s1=new String("kvill");
- String s2=s1.intern();
- System.out.println( s1==s1.intern() );
- System.out.println( s1+" "+s2 );
- System.out.println( s2==s1.intern() );
- 結果:
- false
- kvill kvill
- true
- 在這個類中我們沒有聲名一個”kvill”常量,所以常量池中一開始是沒有”kvill”的,當我們調用s1.intern()後就在常量池中新添加了一個”kvill”常量,原來的不在常量池中的”kvill”仍然存在,也就不是“將自己的地址註冊到常量池中”了。
- s1==s1.intern()爲false說明原來的“kvill”仍然存在;
- s2現在爲常量池中“kvill”的地址,所以有s2==s1.intern()爲true。
- 5. 關於equals()和==:
- 這個對於String簡單來說就是比較兩字符串的Unicode序列是否相當,如果相等返回true;而==是比較兩字符串的地址是否相同,也就是是否是同一個字符串的引用。
- 6. 關於String是不可變的
- 這一說又要說很多,大家只要知道String的實例一旦生成就不會再改變了,比如說:String str=”kv”+”ill”+” “+”ans”;
- 就是有4個字符串常量,首先”kv”和”ill”生成了”kvill”存在內存中,然後”kvill”又和” “ 生成 ”kvill “存在內存中,最後又和生成了”kvill ans”;並把這個字符串的地址賦給了str,就是因爲String的“不可變”產生了很多臨時變量,這也就是爲什麼建議用StringBuffer的原因了,因爲StringBuffer是可改變的
出處:http://www.iteye.com/topic/122206
By the way,關於 String.intern() 在實際中的應用,我在tomcat的源碼中找到了一個地方用到了,如下:
- /*
- * Copyright 1999,2004-2005 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- * ====================================================================
- *
- * This software consists of voluntary contributions made by many
- * individuals on behalf of the Apache Software Foundation and was
- * originally based on software copyright (c) 1999, International
- * Business Machines, Inc., http://www.apache.org. For more
- * information on the Apache Software Foundation, please see
- * <http://www.apache.org/>.
- */
- package org.apache.jasper.xmlparser;
- /**
- * This class is a symbol table implementation that guarantees that
- * strings used as identifiers are unique references. Multiple calls
- * to <code>addSymbol</code> will always return the same string
- * reference.
- * <p>
- * The symbol table performs the same task as <code>String.intern()</code>
- * with the following differences:
- * <ul>
- * <li>
- * A new string object does not need to be created in order to
- * retrieve a unique reference. Symbols can be added by using
- * a series of characters in a character array.
- * </li>
- * <li>
- * Users of the symbol table can provide their own symbol hashing
- * implementation. For example, a simple string hashing algorithm
- * may fail to produce a balanced set of hashcodes for symbols
- * that are <em>mostly</em> unique. Strings with similar leading
- * characters are especially prone to this poor hashing behavior.
- * </li>
- * </ul>
- *
- * @author Andy Clark
- * @version $Id: SymbolTable.java 306179 2005-07-27 15:12:04Z yoavs $
- */
- public class SymbolTable {
- //
- // Constants
- //
- /** Default table size. */
- protected static final int TABLE_SIZE = 101;
- //
- // Data
- //
- /** Buckets. */
- protected Entry[] fBuckets = null;
- // actual table size
- protected int fTableSize;
- //
- // Constructors
- //
- /** Constructs a symbol table with a default number of buckets. */
- public SymbolTable() {
- this(TABLE_SIZE);
- }
- /** Constructs a symbol table with a specified number of buckets. */
- public SymbolTable(int tableSize) {
- fTableSize = tableSize;
- fBuckets = new Entry[fTableSize];
- }
- //
- // Public methods
- //
- /**
- * Adds the specified symbol to the symbol table and returns a
- * reference to the unique symbol. If the symbol already exists,
- * the previous symbol reference is returned instead, in order
- * guarantee that symbol references remain unique.
- *
- * @param symbol The new symbol.
- */
- public String addSymbol(String symbol) {
- // search for identical symbol
- int bucket = hash(symbol) % fTableSize;
- int length = symbol.length();
- OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
- if (length == entry.characters.length) {
- for (int i = 0; i < length; i++) {
- if (symbol.charAt(i) != entry.characters[i]) {
- continue OUTER;
- }
- }
- return entry.symbol;
- }
- }
- // create new entry
- Entry entry = new Entry(symbol, fBuckets[bucket]);
- fBuckets[bucket] = entry;
- return entry.symbol;
- } // addSymbol(String):String
- /**
- * Adds the specified symbol to the symbol table and returns a
- * reference to the unique symbol. If the symbol already exists,
- * the previous symbol reference is returned instead, in order
- * guarantee that symbol references remain unique.
- *
- * @param buffer The buffer containing the new symbol.
- * @param offset The offset into the buffer of the new symbol.
- * @param length The length of the new symbol in the buffer.
- */
- public String addSymbol(char[] buffer, int offset, int length) {
- // search for identical symbol
- int bucket = hash(buffer, offset, length) % fTableSize;
- OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
- if (length == entry.characters.length) {
- for (int i = 0; i < length; i++) {
- if (buffer[offset + i] != entry.characters[i]) {
- continue OUTER;
- }
- }
- return entry.symbol;
- }
- }
- // add new entry
- Entry entry = new Entry(buffer, offset, length, fBuckets[bucket]);
- fBuckets[bucket] = entry;
- return entry.symbol;
- } // addSymbol(char[],int,int):String
- /**
- * Returns a hashcode value for the specified symbol. The value
- * returned by this method must be identical to the value returned
- * by the <code>hash(char[],int,int)</code> method when called
- * with the character array that comprises the symbol string.
- *
- * @param symbol The symbol to hash.
- */
- public int hash(String symbol) {
- int code = 0;
- int length = symbol.length();
- for (int i = 0; i < length; i++) {
- code = code * 37 + symbol.charAt(i);
- }
- return code & 0x7FFFFFF;
- } // hash(String):int
- /**
- * Returns a hashcode value for the specified symbol information.
- * The value returned by this method must be identical to the value
- * returned by the <code>hash(String)</code> method when called
- * with the string object created from the symbol information.
- *
- * @param buffer The character buffer containing the symbol.
- * @param offset The offset into the character buffer of the start
- * of the symbol.
- * @param length The length of the symbol.
- */
- public int hash(char[] buffer, int offset, int length) {
- int code = 0;
- for (int i = 0; i < length; i++) {
- code = code * 37 + buffer[offset + i];
- }
- return code & 0x7FFFFFF;
- } // hash(char[],int,int):int
- /**
- * Returns true if the symbol table already contains the specified
- * symbol.
- *
- * @param symbol The symbol to look for.
- */
- public boolean containsSymbol(String symbol) {
- // search for identical symbol
- int bucket = hash(symbol) % fTableSize;
- int length = symbol.length();
- OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
- if (length == entry.characters.length) {
- for (int i = 0; i < length; i++) {
- if (symbol.charAt(i) != entry.characters[i]) {
- continue OUTER;
- }
- }
- return true;
- }
- }
- return false;
- } // containsSymbol(String):boolean
- /**
- * Returns true if the symbol table already contains the specified
- * symbol.
- *
- * @param buffer The buffer containing the symbol to look for.
- * @param offset The offset into the buffer.
- * @param length The length of the symbol in the buffer.
- */
- public boolean containsSymbol(char[] buffer, int offset, int length) {
- // search for identical symbol
- int bucket = hash(buffer, offset, length) % fTableSize;
- OUTER: for (Entry entry = fBuckets[bucket]; entry != null; entry = entry.next) {
- if (length == entry.characters.length) {
- for (int i = 0; i < length; i++) {
- if (buffer[offset + i] != entry.characters[i]) {
- continue OUTER;
- }
- }
- return true;
- }
- }
- return false;
- } // containsSymbol(char[],int,int):boolean
- //
- // Classes
- //
- /**
- * This class is a symbol table entry. Each entry acts as a node
- * in a linked list.
- */
- protected static final class Entry {
- //
- // Data
- //
- /** Symbol. */
- public String symbol;
- /**
- * Symbol characters. This information is duplicated here for
- * comparison performance.
- */
- public char[] characters;
- /** The next entry. */
- public Entry next;
- //
- // Constructors
- //
- /**
- * Constructs a new entry from the specified symbol and next entry
- * reference.
- */
- public Entry(String symbol, Entry next) {
- this.symbol = symbol.intern();
- characters = new char[symbol.length()];
- symbol.getChars(0, characters.length, characters, 0);
- this.next = next;
- }
- /**
- * Constructs a new entry from the specified symbol information and
- * next entry reference.
- */
- public Entry(char[] ch, int offset, int length, Entry next) {
- characters = new char[length];
- System.arraycopy(ch, offset, characters, 0, length);
- symbol = new String(characters).intern();
- this.next = next;
- }
- } // class Entry
- } // class SymbolTable