1. 程式人生 > >String s3 = "hel" + new String("lo");做了什麼

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

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.

為什麼這麼執行呢?