Difference between x.toString() and x+“”

后端 未结 8 2001
暗喜
暗喜 2020-12-11 22:50

Back in college one of my profs. taught us to just do x + \"\" as a quick conversion from basic types to strings.
I don\'t remember which class it was in I

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-11 23:32

    Because strings are immutable, x+"" invokes two functions: x.toString() and StringBuffer.append() (in Java, at least). I imagine most good JITs would simply turn that into a single function call, x.toString(), but I couldn't be sure without actually testing.

提交回复
热议问题