java字符串的常見(jiàn)問(wèn)題_第1頁(yè)
java字符串的常見(jiàn)問(wèn)題_第2頁(yè)
java字符串的常見(jiàn)問(wèn)題_第3頁(yè)
java字符串的常見(jiàn)問(wèn)題_第4頁(yè)
java字符串的常見(jiàn)問(wèn)題_第5頁(yè)
已閱讀5頁(yè),還剩1頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、constant pool常量池 的概念: 在講到string的一些特殊情況時(shí),總會(huì)提到string pool或者constant pool,但是我想很多人都不太明白constant pool到底是個(gè)怎么樣的東西,運(yùn)行的時(shí)候存儲(chǔ)在哪里,所以在這里先說(shuō)一下constant pool的內(nèi)容. string pool是對(duì)應(yīng)于在constant pool中存儲(chǔ)string常量的區(qū)域.習(xí)慣稱為string pool,也有人稱為string constant pool.好像沒(méi)有正式的命名? 在java編譯好的class文件中,有個(gè)區(qū)域稱為constant pool,他是一個(gè)由數(shù)組組成的表,類型 為cp_i

2、nfo constant_pool,用來(lái)存儲(chǔ)程序中使用的各種常量,包括class / string / integer 等各種基本java數(shù)據(jù)類型,詳情參見(jiàn)the java virtual machine specification 4 .4章節(jié). 關(guān)于string類的說(shuō)明 1 .string使用private final char value來(lái)實(shí)現(xiàn)字符串的存儲(chǔ),也就是說(shuō)string對(duì)象創(chuàng)建之后,就不能再修改此對(duì)象中存儲(chǔ)的字符串內(nèi)容,就是因?yàn)槿绱?才說(shuō)string類型是不可變的(immutable). 2 .string類有一個(gè)特殊的創(chuàng)建方法,就是使用 "" 雙引號(hào)來(lái)創(chuàng)建

3、.例如new string( " i am " )實(shí)際創(chuàng)建了2個(gè)string對(duì)象,一個(gè)是 " i am " 通過(guò) "" 雙引號(hào)創(chuàng)建的,另一個(gè)是通過(guò)new創(chuàng)建的.只不過(guò)他們創(chuàng)建的時(shí)期不同,一個(gè)是編譯期,一個(gè)是運(yùn)行期 ! 3 .java對(duì)string類型重載了 + 操作符,可以直接使用 + 對(duì)兩個(gè)字符串進(jìn)行連接. 4 .運(yùn)行期調(diào)用string類的intern()方法可以向string pool中動(dòng)態(tài)添加對(duì)象. string的創(chuàng)建方法一般有如下幾種 1 .直接使用 "" 引號(hào)創(chuàng)建. 2 .使用new string()創(chuàng)建

4、. 3 .使用new string( " somestring " )創(chuàng)建以及其他的一些重載構(gòu)造函數(shù)創(chuàng)建. 4 .使用重載的字符串連接操作符 + 創(chuàng)建. 例1     java代碼 1. /*   2.     * "sss111"是編譯期常量,編譯時(shí)已經(jīng)能確定它的值,在編譯  3.     * 好的class文件中它已經(jīng)在string pool中了,此語(yǔ)句會(huì)在  4. 

5、60;   * string pool中查找等于"sss111"的字符串(用equals(object)方法確定),  5.     * 如果存在就把引用返回,付值給s1.不存在就會(huì)創(chuàng)建一個(gè)"sss111"放在  6.     * string pool中,然后把引用返回,付值給s1.  7.     *   

6、;8.     */    9.     string s1 = " sss111 "     10.   11.     / 此語(yǔ)句同上    12.     string s2 = " 

7、sss111 "    13.   14.     /*   15.     * 由于string pool只會(huì)維護(hù)一個(gè)值相同的string對(duì)象  16.     * 上面2句得到的引用是string pool中同一個(gè)對(duì)象,所以  17.     * 他們引用相等  1

8、8.     */    19.     system.out.println(s1 = s2); / 結(jié)果為true   /* * "sss111"是編譯期常量,編譯時(shí)已經(jīng)能確定它的值,在編譯 * 好的class文件中它已經(jīng)在string pool中了,此語(yǔ)句會(huì)在 * string pool中查找等于"sss111"的字符串(用equals(object)方法確定), * 如

9、果存在就把引用返回,付值給s1.不存在就會(huì)創(chuàng)建一個(gè)"sss111"放在 * string pool中,然后把引用返回,付值給s1. * */ string s1 = " sss111 " ; / 此語(yǔ)句同上 string s2 = " sss111 " ; /* * 由于string pool只會(huì)維護(hù)一個(gè)值相同的string對(duì)象 * 上面2句得到的引用是string pool中同一個(gè)對(duì)象,所以 * 他們引用相等 */ system.out.println(s1 = s2); / 結(jié)果為true 例2    

10、; java代碼 1. /*   2.     * 在java中,使用new關(guān)鍵字會(huì)創(chuàng)建一個(gè)新對(duì)象,在本例中,不管在  3.     * string pool中是否已經(jīng)有值相同的對(duì)象,都會(huì)創(chuàng)建了一個(gè)新的  4.     * string對(duì)象存儲(chǔ)在heap中,然后把引用返回賦給s1.  5.     * 本例中使用了string的publ

11、ic string(string original)構(gòu)造函數(shù).  6.     */    7.     string s1 = new string( " sss111 " );    8.        9.     /*&#

12、160;  10.      * 此句會(huì)按照例1中所述在string pool中查找  11.      */    12.     string s2 = " sss111 "    13.        14.  &

13、#160;  /*   15.      * 由于s1是new出的新對(duì)象,存儲(chǔ)在heap中,s2指向的對(duì)象  16.      * 存儲(chǔ)在string pool中,他們肯定不是同一個(gè)對(duì)象,只是  17.      * 存儲(chǔ)的字符串值相同,所以返回false.  18.      */

14、60;   19.     system.out.println(s1 = s2); / 結(jié)果為false   /* * 在java中,使用new關(guān)鍵字會(huì)創(chuàng)建一個(gè)新對(duì)象,在本例中,不管在 * string pool中是否已經(jīng)有值相同的對(duì)象,都會(huì)創(chuàng)建了一個(gè)新的 * string對(duì)象存儲(chǔ)在heap中,然后把引用返回賦給s1. * 本例中使用了string的public string(string original)構(gòu)造函數(shù). */ string s1 = new str

15、ing( " sss111 " ); /* * 此句會(huì)按照例1中所述在string pool中查找 */ string s2 = " sss111 " ; /* * 由于s1是new出的新對(duì)象,存儲(chǔ)在heap中,s2指向的對(duì)象 * 存儲(chǔ)在string pool中,他們肯定不是同一個(gè)對(duì)象,只是 * 存儲(chǔ)的字符串值相同,所以返回false. */ system.out.println(s1 = s2); / 結(jié)果為false 例3     java代碼  1. string s1 = ne

16、w string( " sss111 " );   2.   3.    /*   4.     * 當(dāng)調(diào)用intern方法時(shí),如果string pool中已經(jīng)包含一個(gè)等于此string對(duì)象  5.     * 的字符串(用 equals(object)方法確定),則返回池中的字符串.否則,將此  

17、6.     * string對(duì)象添加到池中,并返回此string對(duì)象在string pool中的引用.  7.     */    8.     s1 = ern();    9.        10.     string s2 =&

18、#160;" sss111 "    11.        12.     /*   13.      * 由于執(zhí)行了s1 = ern(),會(huì)使s1指向string pool中值為"sss111"  14.      *

19、0;的字符串對(duì)象,s2也指向了同樣的對(duì)象,所以結(jié)果為true   15.      */    16.     system.out.println(s1 = s2);  string s1 = new string( " sss111 " ); /* * 當(dāng)調(diào)用intern方法時(shí),如果string pool中已經(jīng)包含一個(gè)等于此string對(duì)象 * 的字符串(用 equals(object)方法確

20、定),則返回池中的字符串.否則,將此 * string對(duì)象添加到池中,并返回此string對(duì)象在string pool中的引用. */ s1 = ern(); string s2 = " sss111 " ; /* * 由于執(zhí)行了s1 = ern(),會(huì)使s1指向string pool中值為"sss111" * 的字符串對(duì)象,s2也指向了同樣的對(duì)象,所以結(jié)果為true */ system.out.println(s1 = s2);例4     java代碼 1. string s1 

21、;= new string( " 111 " );    2.     string s2 = " sss111 "    3.        4. /*   5.     * 由于進(jìn)行連接的2個(gè)字符串都是常量,編譯期就

22、能確定連接后的值了,   6.     * 編譯器會(huì)進(jìn)行優(yōu)化 直接把他們表示成"sss111"存儲(chǔ)到string pool中,   7.     * 由于上邊的s2="sss111"已經(jīng)在string pool中加入了"sss111",   8.     * 此句會(huì)把s3指向和s2相同的對(duì)象,所以他

23、們引用相同.此時(shí)"sss"和"111"   9.     * 兩個(gè)常量不會(huì)再創(chuàng)建.   10.     */    11.   12.     string s3 = " sss " + " 111 "

24、0;   13.        14.     /*   15.      * 由于s1是個(gè)變量,在編譯期不能確定它的值是多少 ,所以  16.      * 會(huì)在執(zhí)行的時(shí)候創(chuàng)建一個(gè)新的string對(duì)象存儲(chǔ)到heap中,  17.      * 然后

25、賦值給s4.  18.      */    19.     string s4 = " sss " + s1;   20.        21.     system.out.println(s2 = s3); / t

26、rue    22.     system.out.println(s2 = s4); / false    23.     system.out.println(s2 = ern(); / true  string s1 = new string( " 111 " ); string s2 = " sss111 &

27、quot; ; /* * 由于進(jìn)行連接的2個(gè)字符串都是常量,編譯期就能確定連接后的值了, * 編譯器會(huì)進(jìn)行優(yōu)化 直接把他們表示成"sss111"存儲(chǔ)到string pool中, * 由于上邊的s2="sss111"已經(jīng)在string pool中加入了"sss111", * 此句會(huì)把s3指向和s2相同的對(duì)象,所以他們引用相同.此時(shí)"sss"和"111" * 兩個(gè)常量不會(huì)再創(chuàng)建. */ string s3 = " sss " + " 111 " ; /* *

28、由于s1是個(gè)變量,在編譯期不能確定它的值是多少 ,所以 * 會(huì)在執(zhí)行的時(shí)候創(chuàng)建一個(gè)新的string對(duì)象存儲(chǔ)到heap中, * 然后賦值給s4. */ string s4 = " sss " + s1; system.out.println(s2 = s3); / true system.out.println(s2 = s4); / false system.out.println(s2 = ern(); / true例5 這個(gè)是the java language specification中3. 10 .5節(jié)的例子,有了上面的說(shuō)明,這個(gè)應(yīng)該不難理解了 

29、0;   java代碼 1. package testpackage;   2.     class test    3.             public static void main(string args)    4.     

30、;                string hello = " hello " , lo = " lo "    5.            &#

31、160;        system.out.print(hello = " hello " ) + " " );   6.                     sy

32、stem.out.print(other.hello = hello) + " " );   7.                     system.out.print(other.other.hello = hello) + " &q

33、uot; );   8.                     system.out.print(hello = ( " hel " + " lo " ) + " "); 

34、60; 9.                     system.out.print(hello = ( " hel " + lo) + " " );  /lo在runtime會(huì)創(chuàng)建一個(gè)新的對(duì)象   

35、;10.                     system.out.println(hello = ( " hel " + lo).intern();   11.           &

36、#160;    12.        13.     class other  static string hello = " hello "     14.   15.     package other;   16. 

37、60;   public class other  static string hello = " hello "    package testpackage; class test public static void main(string args) string hello = " hello " , lo = " lo " ; system.out.pri

38、nt(hello = " hello " ) + " " ); system.out.print(other.hello = hello) + " " ); system.out.print(other.other.hello = hello) + " " ); system.out.print(hello = ( " hel " + " lo " ) + " "); system.out.print(hello = ( " hel " + lo) + " " ); /lo在runtime會(huì)創(chuàng)建一個(gè)新的對(duì)象 system.out.println(hello = ( " hel " + lo).intern(); cl

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論