String s3 = "hel" + new String("lo");做了什麼

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/thirtyfive16/article/details/53258591

String s3 = "hel" + new String("lo");底層到底是如何執行的呢?

寫個程序,用eclipse做debug可以得到詳細的執行的順序

1 load StringBuilder類

2 生成臨時變量StringBuilder,保存"hel"字符串

3 執行String(String)的構造函數,得到字符串"lo"

4 執行StringBuilder.append() 把"lo” append到“hel”上得到字符串“hello”

5 執行StringBuilder.toString() 返回"hello”給變量S3.


爲什麼這麼執行呢?



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章