String字串拼接陷阱

2021-06-26 20:43:41 字數 469 閱讀 1460

先看如下程式:

對於一般類物件:

public class a

}public class test

public static void main(string args)

再看如下程式:

public class test

public static void main(string args)

}輸出的結果實多少? helloworld?, 

實際上是:hello

//我們在看以下程式你就知道為什麼輸出的是hello,而不是helloworld了

public class test

}結果是:

48690

1450575459

原來他們並不是同乙個物件的引用,他們的值發生了變化。

原因:string型別資料其實是final型別,它的值並不能改變!要改變的其值就是:重新生成乙個物件。

String字串拼接速率

在for迴圈中,比較常用的字串拼接方式包括以下五種方式 string,string.concat apache.commons.lang3.stringutils.join stringbuffer stringbuilder 接下來,依次分析上述五種方式 string,public final s...

String 型別字串儲存和拼接

string str1 hello string str2 he llo string str3 he new string llo str1 str2 true str1 str3 false 這個問題我試著回答一下,同時也是相互學習。string str1 hello stringstr2 he...

精 (String)字串拼接記憶體解析

string str1 hello 字串常量池中沒有 hello 則建立乙個字串並把位址值返回給str1 string str2 hello 字串常量池中存在 hello 則創將該字串的位址值返回給str2 string str3 new string hello str3指向物件的位址,物件中的值...